配置 yum 源
在https://dev.mysql.com/downloads/repo/yum/找到 yum 源 rpm 安装包
安装MySQL源
# 下载
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 安装 mysql 源
yum localinstall mysql57-community-release-el7-11.noarch.rpm
检查 mysql 源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
安装MySQL
yum install -y mysql-community-server
启动MySQL服务
systemctl start mysqld
查看MySQL状态
systemctl status mysqld
设置开机启动
systemctl enable mysqld
# 重载所有修改过的配置文件
systemctl daemon-reload
查看登陆密码
mysql 安装完成之后,生成的默认密码在 /var/log/mysqld.log
文件中,使用 grep 命令找到日志中的密码
grep 'temporary password' /var/log/mysqld.log
首次通过初始密码登录后,使用以下命令修改密码
mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
或者
mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
也可以使用 update set 语句修改密码
mysql> use mysql;
mysql> update user set password=PASSWORD('MyNewPass5!') where user='root';
mysql> flush privileges;
MySQL5.6.6版本之后增加了密码强度验证插件validate_password,默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 错误。使用了该插件会检查设置的密码是否符合当前设置的强度规则,若不满足则拒绝设置。所以需要修改密码强度的验证机制:
如果你想设置为弱密码,也可以通过几条命令实现修改
1) 查看mysql全局参数配置
这里与mysql的validate_password_policy的值有关
查看一下msyql密码相关的几个全局参数:
mysql> select @@validate_password_policy;
+----------------------------+
| @@validate_password_policy |
+----------------------------+
| MEDIUM |
+----------------------------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
6 rows in set (0.08 sec)
2)参数解释
validate_password_dictionary_file
插件用于验证密码强度的字典文件路径
validate_password_length
密码最小长度,参数默认为8,它有最小值的限制,最小值为:validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)
validate_password_mixed_case_count
密码至少要包含的小写字母个数和大写字母个数
validate_password_number_count
密码至少要包含的数字个数
validate_password_policy
密码强度检查等级,0/LOW、1/MEDIUM、2/STRONG。有以下取值:
Policy Tests Performed
0 or LOW Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符
validate_password_special_char_count
密码至少要包含的特殊字符数
3)修改mysql参数配置
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.05 sec)
mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_number_count=0;
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=0;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
|validate_password_dictionary_file | |
| validate_password_length | 0 |
| validate_password_mixed_case_count | 0 |
| validate_password_number_count | 0 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 0 |
+--------------------------------------+-------+
6 rows in set (0.00 sec)
4)修改简单密码
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
配置远程登陆
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须添加一个允许远程连接的帐户。或者修改 root 为允许远程连接,不推荐
添加一个允许远程连接的帐户
mysql> GRANT ALL PRIVILEGES ON *.* TO '账号'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;
修改 root 为允许远程连接(不推荐)
mysql> use mysql;
mysql> UPDATE user SET Host='%' WHERE User='root';
mysql> flush privileges;
设置默认编码为 utf8
mysql 安装后默认不支持中文,需要修改编码。
修改 /etc/my.cnf
配置文件,在相关节点(没有则自行添加)下添加编码配置,如下:
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
重启mysql服务,查询编码。可以看到已经改过来了
systemctl restart mysqld
mysql -uroot -p
mysql> show variables like 'character%';