下载

安装

使用 Yum 安装MySQL

安装步骤

检查MySQL

  1. rpm -qa|grep mysql

删除MySQL
如果不存在,则跳过此步骤

  1. rpm -e --nodeps xxx

添加MySQL Yum Resposity

从CentOS 7开始,MariaDB成为Yum源中默认的数据库安装包。也就是说在CentOS 7及以上的系统中使用yum安装MySQL默认安装的会是MariaDB(MySQL的一个分支)。如果想安装官方MySQL版本,需要使用MySQL提供的Yum源。

1. 下载MySQL源

MySQL官网地址
查看系统版本

  1. shell> cat /etc/redhat-release

选择对应的版本进行下载,例如CentOS 7当前在官网查看最新Yum源的下载地址为: dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

  1. shell> wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

2. 安装MySQL源

  1. sudo yum install mysql-community-server

该命令会安装MySQL服务器 (mysql-community-server) 及其所需的依赖、相关组件,包括mysql-community-client、mysql-community-common、mysql-community-libs等
如果带宽不够,这个步骤时间会比较长,请耐心等待~

启动MySQL

启动MySQL

  1. # CentOS 7
  2. shell> sudo systemctl start mysqld.service
  3. # CentOS 6
  4. shell> sudo service mysqld start

查看MySQL启动状态

  1. # CentOS 7
  2. shell> sudo systemctl status mysqld.service

停止MySQL

  1. # CentOS 7
  2. shell> sudo systemctl stop mysqld.service
  3. # CentOS 6
  4. shell> sudo service mysqld stop

重启MySQL

  1. # CentOS 7
  2. shell> sudo systemctl restart mysqld.service
  3. # CentOS 6
  4. shell> sudo service mysqld restart

修改密码

初始密码

MySQL第一次启动后会创建超级管理员账号root@localhost,初始密码存储在日志文件中:

  1. shell> sudo grep 'temporary password' /var/log/mysqld.log

修改默认密码

  1. shell> mysql -uroot -p
  1. 方法一:用UPDATE直接编辑user表,或者使用ALTER编辑user表
    1. mysql> UPDATE user SET password=PASSWORD('newpassword') where USER='root';
  1. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
  2. ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

出现上面的提示是因为密码太简单了,解决方法如下:

  1. 使用复杂密码,MySQL默认的密码策略是要包含数字、字母及特殊字符;
  2. 如果只是测试用,不想用那么复杂的密码,可以修改默认策略,即validate_password_policy(以及validate_password_length等相关参数),使其支持简单密码的设定,具体方法可以自行百度;
  3. 修改配置文件/etc/my.cnf,添加validate_password=OFF,保存并重启MySQL

    允许MySQL远程访问

    1. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
    2. mysql> FLUSH PRIVILEGES;

    设置编码为utf8

    查看编码
    1. mysql> SHOW VARIABLES LIKE 'character%';
    设置编码
    编辑/etc/my.cnf,[mysqld]节点增加以下代码:
    1. [mysqld]
    2. character_set_server=utf8
    3. init-connect='SET NAMES utf8'

    设置开机启动

    1. shell> systemctl enable mysqld
    2. shell> systemctl daemon-reload

    遇到的问题

参考链接:CentOS安装MySQL详解