亲测:https://www.cnblogs.com/zhangliuhero/p/13927823.html
看系统是否已经安装mysql
yum list installed | grep mysql
若出现,则卸载旧的
yum -y remove mysql-libs.x86_64
安装mysql yum源
yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
安装mysql5.7
yum install -y mysql-community-server
启动mysql和开机自启
systemctl start mysqld.service
systemctl enable mysqld.service
密码问题:
由于MySQL从5.7开始不允许首次安装后使用空密码进行登录!为了加强安全性,系统会随机生成一个密码以供管理员首次登录使用,这个密码记录在/var/log/mysqld.log文件中,使用下面的命令可以查看此密码:
cat /var/log/mysqld.log|grep 'A temporary password'
登录mysql
mysql -p
- 使用随机生成的密码登录,会提示修改密码才能后续操作.
show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
- 有两种方式修改密码
set password=password("123456");
或者
alter user 'root'@'localhost' identified by '123456';
注意:若报错:ERROR 1819(HY000): Your password does not statisfy the current policy requirements
这个其实与validate_password_policy的值有关,默认的数值是1,符合长度,且必须含有数字,小写或大写字母,特殊字符。
validate_password_policy有以下取值:
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,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。须修改两个全局参数:validate_password_policy、>validate_password_length 其中,validate_password_number_count指定了密码中数据的长度,validate_password_special_char_count指定了密码中特殊字符的长度,validate_password_mixed_case_count指定了密码中大小字母的长度。这些参数,默认值均为1,所以validate_password_length最小值为4,如果你显性指定validate_password_length的值小于4,尽管不会报错,但validate_password_length的值将设为4
//当前是在mysql用户的前提下
set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
刷新权限
flush privileges;
Query OK, 0 rows affected (0.00 sec)
注意一点:
mysql5.7之后的数据库里mysql.user表里已经没有password这个字段了,password字段改成了authentication_string。所以修改密码的命令如下:
update mysql.user set authentication_string=password('123456') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
查看mysql版本
select version();
+-----------+
| version() |
+-----------+
| 5.7.32 |
+-----------+
1 row in set (0.01 sec)
注意:默认安装的MySQL是不允许远程连接的,需要修改参数
//登录mysql
[root@instance-8jlyr1f4 zhangliu]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.32 MySQL Community Server (GPL)
//切换到mysql
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
//修改所有ip可以远程mysql
mysql> update user set host ='%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
//刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)