CentOs7安装MySQL5.7
5.7相较于5.6来说,坑很多 因此写教程记录一下,以后用得上
1.通过ssh连接服务器
2.安装Mysql
运行以下命令更新YUM源。
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
- 运行以下命令安装MySQL。(镜像可能会稍微有点久,多等一会)
yum -y install mysql-community-server
- 运行以下命令查看MySQL版本号。
mysql -v
返回结果如下,表示MySQL安装成功。mysql Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using EditLine wrapper
3.配置MySQL
- 运行以下命令启动MySQL服务。
systemctl start mysqld
- 运行以下命令设置MySQL服务开机自启动。
systemctl enable mysqld
- 运行以下命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码。(下一步重置root用户密码时,会使用该初始密码)
grep 'temporary password' /var/log/mysqld.log
2019-11-13T09:22:30.998148Z 1 [Note] A temporary password is generated for root@localhost: c(dwuSJQP0pq
- 运行下列命令对MySQL进行安全性配置。
mysql_secure_installation
4.配置MySQL的安全性
- 重置root用户的密码。
Enter password for user root: #输入上一步获取的root用户初始密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y
New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
Re-enter new password: #再次输入新密码
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
- 输入Y删除匿名用户账号。
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y
Success.
- 输入Y禁止root账号远程登录。
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
Success.
- 输入Y删除test库以及对test库的访问权限。
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
- Dropping test database...
Success.
- 输入Y重新加载授权表。
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
Success.
All done!
5.远程访问MySQL数据库
- 通过以下命令登录mysql
mysql -uroot -p
- 依次运行以下命令创建远程登录MySQL的账号。示例账号为root、密码为root。
mysql> grant all on *.* to 'root'@'%'IDENTIFIED BY 'root'; #使用root替换dms,可设置为允许root账号远程登录。
mysql> flush privileges;
简单密码修改方式
- 通过以下命令登录mysql
mysql -uroot -p
- 通过以下命令修改mysql默认的密码安全性
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql>
mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_number_count=3;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=3;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_dictionary_file | |
| validate_password_length | 3 |
| validate_password_mixed_case_count | 0 |
| validate_password_number_count | 3 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 0 |
+--------------------------------------+-------+
- 修改简单密码:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)