1 下载并安装MySQL官方的 Yum Repository

  1. [root@instance ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。

  1. [root@instance ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm

之后就开始安装MySQL服务器。

  1. [root@instance ~]# yum -y install mysql-community-server
  2. Loaded plugins: langpacks, versionlock
  3. mysql-connectors-community | 2.5 kB 00:00:00
  4. mysql-tools-community | 2.5 kB 00:00:00
  5. mysql57-community | 2.5 kB 00:00:00
  6. (1/3): mysql-connectors-community/x86_64/primary_db | 41 kB 00:00:00
  7. (2/3): mysql-tools-community/x86_64/primary_db | 58 kB 00:00:00
  8. (3/3): mysql57-community/x86_64/primary_db | 177 kB 00:00:01
  9. Resolving Dependencies
  10. --> Running transaction check
  11. ---> Package mysql-community-server.x86_64 0:5.7.26-1.el7 will be installed
  12. --> Processing Dependency: mysql-community-common(x86-64) = 5.7.26-1.el7 for package: mysql-community-server-5.7.26-1.el7.x86_64
  13. --> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.26-1.el7.x86_64
  14. --> Running transaction check
  15. ---> Package mysql-community-client.x86_64 0:5.7.26-1.el7 will be installed
  16. --> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.26-1.el7.x86_64
  17. ---> Package mysql-community-common.x86_64 0:5.7.26-1.el7 will be installed
  18. --> Running transaction check
  19. ---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be obsoleted
  20. --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
  21. --> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
  22. ---> Package mysql-community-libs.x86_64 0:5.7.26-1.el7 will be obsoleting
  23. --> Running transaction check
  24. ---> Package mysql-community-libs-compat.x86_64 0:5.7.26-1.el7 will be obsoleting
  25. ---> Package postfix.x86_64 2:2.10.1-6.el7 will be updated
  26. ---> Package postfix.x86_64 2:2.10.1-7.el7 will be an update
  27. --> Finished Dependency Resolution
  28. Dependencies Resolved
  29. ===============================================================================================================
  30. Package Arch Version Repository Size
  31. ===============================================================================================================
  32. Installing:
  33. mysql-community-libs x86_64 5.7.26-1.el7 mysql57-community 2.2 M
  34. replacing mariadb-libs.x86_64 1:5.5.56-2.el7
  35. mysql-community-libs-compat x86_64 5.7.26-1.el7 mysql57-community 2.0 M
  36. replacing mariadb-libs.x86_64 1:5.5.56-2.el7
  37. mysql-community-server x86_64 5.7.26-1.el7 mysql57-community 166 M
  38. Installing for dependencies:
  39. mysql-community-client x86_64 5.7.26-1.el7 mysql57-community 24 M
  40. mysql-community-common x86_64 5.7.26-1.el7 mysql57-community 274 k
  41. Updating for dependencies:
  42. postfix x86_64 2:2.10.1-7.el7 base 2.4 M
  43. Transaction Summary
  44. ===============================================================================================================
  45. Install 3 Packages (+2 Dependent packages)
  46. Upgrade ( 1 Dependent package)
  47. Total download size: 197 M
  48. Downloading packages:
  49. No Presto metadata available for base
  50. warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
  51. Public key for mysql-community-common-5.7.26-1.el7.x86_64.rpm is not installed
  52. (1/6): mysql-community-common-5.7.26-1.el7.x86_64.rpm | 274 kB 00:00:05
  53. (2/6): mysql-community-client-5.7.26-1.el7.x86_64.rpm | 24 MB 00:00:34
  54. (3/6): mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm | 2.0 MB 00:00:02
  55. (4/6): postfix-2.10.1-7.el7.x86_64.rpm | 2.4 MB 00:00:00
  56. (5/6): mysql-community-libs-5.7.26-1.el7.x86_64.rpm | 2.2 MB 00:03:08
  57. (6/6): mysql-community-server-5.7.26-1.el7.x86_64.rpm | 166 MB 00:02:40
  58. ---------------------------------------------------------------------------------------------------------------
  59. Total 1.0 MB/s | 197 MB 00:03:17
  60. Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  61. Importing GPG key 0x5072E1F5:
  62. Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
  63. Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
  64. Package : mysql57-community-release-el7-10.noarch (installed)
  65. From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  66. Running transaction check
  67. Running transaction test
  68. Transaction test succeeded
  69. Running transaction
  70. Installing : mysql-community-common-5.7.26-1.el7.x86_64 1/8
  71. Installing : mysql-community-libs-5.7.26-1.el7.x86_64 2/8
  72. Installing : mysql-community-client-5.7.26-1.el7.x86_64 3/8
  73. Installing : mysql-community-libs-compat-5.7.26-1.el7.x86_64 4/8
  74. Updating : 2:postfix-2.10.1-7.el7.x86_64 5/8
  75. Installing : mysql-community-server-5.7.26-1.el7.x86_64 6/8
  76. Cleanup : 2:postfix-2.10.1-6.el7.x86_64 7/8
  77. Erasing : 1:mariadb-libs-5.5.56-2.el7.x86_64 8/8
  78. Verifying : mysql-community-server-5.7.26-1.el7.x86_64 1/8
  79. Verifying : mysql-community-client-5.7.26-1.el7.x86_64 2/8
  80. Verifying : 2:postfix-2.10.1-7.el7.x86_64 3/8
  81. Verifying : mysql-community-libs-5.7.26-1.el7.x86_64 4/8
  82. Verifying : mysql-community-common-5.7.26-1.el7.x86_64 5/8
  83. Verifying : mysql-community-libs-compat-5.7.26-1.el7.x86_64 6/8
  84. Verifying : 1:mariadb-libs-5.5.56-2.el7.x86_64 7/8
  85. Verifying : 2:postfix-2.10.1-6.el7.x86_64 8/8
  86. Installed:
  87. mysql-community-libs.x86_64 0:5.7.26-1.el7 mysql-community-libs-compat.x86_64 0:5.7.26-1.el7
  88. mysql-community-server.x86_64 0:5.7.26-1.el7
  89. Dependency Installed:
  90. mysql-community-client.x86_64 0:5.7.26-1.el7 mysql-community-common.x86_64 0:5.7.26-1.el7
  91. Dependency Updated:
  92. postfix.x86_64 2:2.10.1-7.el7
  93. Replaced:
  94. mariadb-libs.x86_64 1:5.5.56-2.el7
  95. Complete!

2 MySQL数据库设置

首先启动MySQL

  1. [root@instance ~]# systemctl start mysqld.service

查看MySQL运行状态,运行状态如图:

  1. [root@instance ~]# systemctl status mysqld.service

image.png

  1. [root@instance ~]# grep "password" /var/log/mysqld.log

image.png

如下命令进入数据库:

  1. [root@localhost ~]# mysql -uroot -p

输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:

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

这里有个问题,新密码设置的时候如果设置的过于简单会报错:
CentOS7 yum方式安装MySQL5.7 - 图3
原因是因为MySQL有密码设置的规范,具体是与validate_password_policy的值有关:
CentOS7 yum方式安装MySQL5.7 - 图4

  1. mysql> SHOW VARIABLES LIKE 'validate_password%';
  2. +--------------------------------------+-------+
  3. | Variable_name | Value |
  4. +--------------------------------------+-------+
  5. | validate_password_check_user_name | OFF |
  6. | validate_password_dictionary_file | |
  7. | validate_password_length | 4 |
  8. | validate_password_mixed_case_count | 1 |
  9. | validate_password_number_count | 1 |
  10. | validate_password_policy | LOW |
  11. | validate_password_special_char_count | 1 |
  12. +--------------------------------------+-------+
  13. rows in set (0.01 sec)

密码的长度是由validate_password_length决定的,而validate_password_length的计算公式是:

  1. validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)

我的是已经修改过的,初始情况下第一个的值是ON,validate_password_length是8。可以通过如下命令修改:

  1. mysql> set global validate_password_policy=0;
  2. mysql> set global validate_password_length=1;

设置之后就是我上面查出来的那几个值了,此时密码就可以设置的很简单,例如1234之类的。到此数据库的密码设置就完成了。
但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:

  1. [root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch

使远程可以连接mysql

  1. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;
  2. flush privileges;