在 Linux 中安装 mysql,记录一下方便查看。

如果不想看太长的文字,可以直接看下文的总结。

官方推荐使用 yum 装,这里记录的是 rpm 包安装的方式。

机器是阿里云的云主机,64 位 Centos 7.6,安装的 mysql 版本是 Community 5.7.28。

以下的命令都是以 root 用户运行。

1
2
$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

首先下载 rpm 包,下载地址:

https://dev.mysql.com/downloads/mysql/

选择好版本,再选择 bundle 版本包的链接,下载并解压:

1
2
3
4
5
6
7
8
9
10
11
12
$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
$ tar -xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-5.7.28-1.el7.x86_64.rpm
mysql-community-test-5.7.28-1.el7.x86_64.rpm
mysql-community-common-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-client-5.7.28-1.el7.x86_64.rpm
mysql-community-server-5.7.28-1.el7.x86_64.rpm

解压出了几个 rpm 包,等下会按顺序安装。

查看是否已经安装 mysql

1
$ rpm -qa | grep mysql

如果没有任何的值返回则说明还没有安装 mysql。

依次安装 rpm 包

需要安装顺序安装包,顺序如下:

1
2
3
4
5
6
7
8
9
10
$ rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-devel-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-test-5.7.28-1.el7.x86_64.rpm

在安装 mysql-community-libs-5.7.28-1.el7.x86_64.rpm 时会有报错:

1
2
3
4
5
$ rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
mysql-community-common(x86-64) >= 5.7.9 被 mysql-community-libs-5.7.28-1.el7.x86_64 需要
mariadb-libs 被 mysql-community-libs-5.7.28-1.el7.x86_64 取代

如果是英文版,意思应该是一样的。

这是因为有冲突,把原本的 mariadb-libs 卸载即可:

1
2
3
$ rpm -qa | grep mariadb-libs
mariadb-libs-5.5.60-1.el7_5.x86_64
$ yum remove mariadb-libs-5.5.60-1.el7_5.x86_64

卸载完之后再重新按顺序安装。

在安装 mysql-community-server-5.7.28-1.el7.x86_64.rpm 的时候提示:

1
2
3
4
5
6
$ rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
libaio.so.1()(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要
libaio.so.1(LIBAIO_0.1)(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要
libaio.so.1(LIBAIO_0.4)(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要

需要安装 libaio 依赖:

1
$ yum install libaio

再接着安装 mysql-community-server-5.7.28-1.el7.x86_64.rpm 就不会有错误提示了。

在安装到 mysql-community-test-5.7.28-1.el7.x86_64 时会提示:

1
2
3
4
5
$ rpm -ivh mysql-community-test-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-test-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
perl(Data::Dumper) 被 mysql-community-test-5.7.28-1.el7.x86_64 需要
perl(JSON) 被 mysql-community-test-5.7.28-1.el7.x86_64 需要

需要安装这两个依赖:

1
$ yum install perl-Data-Dumper perl-JSON

安装完之后再继续安装。

到这里所有的包就安装完了,验证 mysql 是否已经安装:

1
$ mysqld --version

启动 mysqld 守护进程:

1
$ systemctl start mysqld.service

如果没有意外,则能够正常启动。

如果启动不成功,有以下提示:

1
2
$ systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

运行以下命令修改 mysql 数据目录所有权:

1
$ chown mysql:mysql -R /var/lib/mysql

如无意外,修改之后则可以正常启动。

修改密码

mysql 在第一次启动 mysqld 时候会生成一个随机临时密码用于 root 用户登录(注意是 mysql 的 root 用户,不是 Linux 的 root 用户),先查看随机临时密码:

1
2
$ grep 'temporary password' /var/log/mysqld.log
2019-10-27T12:53:16.015895Z 1 [Note] A temporary password is generated for root@localhost: xxxxxxxxxxx

用这个密码登录再修改密码:

1
2
$ mysql -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'hahaha';

设置密码时有可能会出现如下的提示:

1
2
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'hahaha';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

是因为输入的密码不符合默认的 validate_password_policy,可以通过以下命令查看密码的要求:

1
mysql> SHOW VARIABLES LIKE 'validate_password%';

validate_password_policy 的默认值为 MEDIUM,则密码需要满足以下的 validation

  • validate_password_length
  • validate_password_mixed_case_count
  • validate_password_number_count
  • validate_password_special_char_count

总结起来就是,密码长度大于或等于 8,其中需要有英文大小写、数字以及特殊字符。

改一下输入的密码:

1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Haha123.';

如无意外,设置密码就成功了。

总结

先看看是否已经有装 mysql 或者 mariadb,如果有的话就先移除:

1
2
3
$ rpm -qa | grep mariadb-libs
$ rpm -qa | grep mysql-libs
$ yum remove mariadb-libs-5.5.60-1.el7_5.x86_64

再下载 rpm 包并解压:

1
2
3
4
5
6
7
8
9
10
11
12
$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
$ tar -xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-5.7.28-1.el7.x86_64.rpm
mysql-community-test-5.7.28-1.el7.x86_64.rpm
mysql-community-common-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-client-5.7.28-1.el7.x86_64.rpm
mysql-community-server-5.7.28-1.el7.x86_64.rpm

安装必须的依赖:

1
$ yum install -y libaio perl-Data-Dumper perl-JSON

按照顺序安装:

1
$ rpm -ivh mysql-community-{common,libs,client,server,devel,embedded,test}-*.rpm

验证是否安装成功

1
2
$ mysqld --version
mysqld Ver 5.7.28 for Linux on x86_64 (MySQL Community Server (GPL))

运行 mysqld 守护进程

1
$ systemctl start mysqld.service

查看生成的临时密码

1
2
$ grep 'temporary password' /var/log/mysqld.log
2019-10-27T14:37:17.500076Z 1 [Note] A temporary password is generated for root@localhost: xxxxxxxxx

使用临时登录 mysql

1
$ mysql -p

修改 mysql root 用户密码

修改的密码必须满足:密码长度大于或等于 8,其中需要有英文大小写、数字以及特殊字符。

1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Haha123.';

参考

2.5.5 Installing MySQL on Linux Using RPM Packages from Oracle

6.4.3.2 Password Validation Plugin Options and Variables

MySQL 版本 Enterprise/Community/Cluster 等版本的区别

MySQL Bugs: #72926: MySQL-server RPM does not install perl-Data-Dumper as a dependency