总所周知,MySQL 被 Oracle 收购后,CentOS 的镜像仓库中提供的默认的数据库也变为了 MariaDB
在 CentOS 上安装 MySQL步骤及注意如下
1. 添加 MySQL YUM 源
根据自己的操作系统选择合适的安装源,下载之后通过 rpm -Uvh
安装。

可以拿到yum源地址
$wget 'https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm' #下载yum源
$sudo rpm -Uvh mysql80-community-release-el7-1.noarch.rpm #安装源(按tab可补全后面)
$yum repolist all | grep mysql

也就是说这个安装源包含了上面列举的这些版本,当然包括 5.6 版本的。
2. 选择安装版本
如果想安装最新版本的,直接使用 yum 命令即可
$sudo yum install mysql-community-server
如果想要安装 5.6 版本的,有2个方法。命令行支持 yum-config-manager 命令的话,可以使用如下命令:
$ sudo dnf config-manager --disable mysql57-community
$ sudo dnf config-manager --enable mysql56-community
$ yum repolist | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 36
mysql-tools-community/x86_64 MySQL Tools Community 47
mysql56-community/x86_64 MySQL 5.6 Community Server 327
或者直接修改 /etc/yum.repos.d/mysql-community.repo 这个文件

设置好之后再使用上面的 yum 安装命令即可。
3.启动sql服务
$sudo service mysqld start #CentOS 7 $sudo systemctl start mysqld
$sudo systemctl status mysqld

说明已经在运行中了。
对于 MySQL 5.7 版本,启动的时候如果数据为空的,则会出现如下提示
The server is initialized.
An SSL certificate and key files are generated in the data directory.
The validate_password plugin is installed and enabled.
A superuser account 'root'@'localhost' is created. A password for the superuser is set and stored in the error log file. To reveal it, use the following command:
sudo grep 'temporary password' /var/log/mysqld.log
////简单的说就是服务安装好了,SSL 认证的文件会在 data 目录中生存,密码不要设置的太简单了,初始密码通过下面的命令查看,赶紧去改密码吧。
上面的命令查看密码,登录数据库,然后修改密码:
$ mysql -uroot -p #输入查看到的密码 如果上面命令没有返回密码(mysql5.6)说明没有密码,则直接回车
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'; #非5.6 5.6下面方式设置
MySQL 5.6 的安全设置
由于 5.7 版本在安装的时候就设置好了,不需要额外设置,但是 5.6 版本建议从安全角度完善下,运行官方脚本即可 : $ mysql_secure_installation
会提示设置5个关键位置
- 设置 root 密码
- 禁止 root 账号远程登录
- 禁止匿名账号(anonymous)登录
- 删除测试库
- 是否确认修改
4. 开放其他地址连接
MySQL 安装后,默认只开放本机连接,为了支持其他主机连接,可以通过修改表mysql的host字段来实现。
mysql -u root -p
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
// 如果报Duplicate entry '%-root' for key 'PRIMARY'
//不用理会 用 use mysql; select user, host from user; 查看是否已经有 user=‘root’ 的 host 变成 %
mysql>flush privileges; //记得刷新
修改编码
在 /etc/my.cnf 中设置默认的编码
[client]
default-character-set = utf8
[mysqld]
default-storage-engine = INNODB
character-set-server = utf8
collation-server = utf8_general_ci #不区分大小写
collation-server = utf8_bin #区分大小写
collation-server = utf8_unicode_ci #比 utf8_general_ci 更准确
网友评论