CentOS7安装mysql
in with 0 comment

CentOS7安装mysql

in with 0 comment

CentOS7安装mysql5.7

准备工作

  1. 检查是否安装mysql/mariadb

    $ rpm -qa | grep mariadb
    

    如果已经安装,卸载,否则继续

    $ rpm -e --nodeps mariadb-libs*
    
  2. 下载noarch包

    $ wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    
  3. 安装noarch包

    $ rpm -ivh mysql80-community-release-el7-3.noarch.rpm
    
  4. 修改配置/etc/yum.repos.d/mysql-community.repo

    [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=0 #把这里的0改为1,打开5.7 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

    [mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled=1 #把这里的1改为0,关闭8.0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

  5. 查看所有mysql版本

    $ yum repolist all | grep mysql
    
  6. 查看当前启用的mysql版本

    $ yum repolist enabled | grep mysql
    

安装

  1. 安装mysql

    $ yum install mysql-community-server -y
    
  2. 查看mysql版本

    $ mysql --version
    

配置

  1. 启动mysql

    $ systemctl start mysqld.service
    
  2. 关闭mysql

    $ systemctl stop mysqld.service
    
  3. 获取默认产生的随机密码

    $ grep 'temporary password' /var/log/mysqld.log
    
  4. 登陆MySQL以及修改root密码

    $ mysql -uroot -p
    
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
    或
    mysql> set password for 'root'@'localhost'=password('password'); 
    
  5. 新建数据库以及用户,并赋予相应权限

    mysql> create database dev character set utf8 collate utf8_general_ci; #新建数据库
    msyql> create user 'dev'@'%' identified by 'password'; #新建能远程登陆的用户
    mysql> grant all privileges on dev.* to '123456'@'%' with grant option; #授权dev用户拥有dev数据库所有权限
    mysql> flush privileges; #刷新
    
  6. 开机启动

    $ systemctl enable mysqld