Linux在线安装MySQL5.7
Linux在线安装MySQL 5.7及编码密码修改
先检测系统是否自带安装mysql
yum list installed | grep mysql
若出现mysql相关信息,卸载原始版本
yum -y remove mysql-libs.x86_64
安装MySQL YUM资源库
yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum module disable mysql
安装MySQL 5.7(由于是官网资源,下载较慢)
yum install -y mysql-community-server
启动MySQL服务器和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'
2020-11-04T11:34:58.003571Z 1 [Note] A temporary password is generated for root@localhost: WcGdYe!e76%
最后一行冒号后面的部分WcGdYe!e76%就是初始密码。
使用此密码登录MySQL:
mysql -p
注意:使用随机生产的密码登录到服务端后,必须马上修改密码,不然会报如下错误:
show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
有两种方法解决上面的报错(如下的123456是修改后的密码):
set password=password("123456");
或者
alter user 'root'@'localhost' identified by '123456';
注意:若报错:ERROR 1819 (HY000): Your password does not satisfy 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)
如果修改了validate_password_number_count,validate_password_special_char_count,validate_password_mixed_case_count中任何一个值,则validate_password_length将进行动态修改。
执行完后再执行修改密码的操作。
刷新权限
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)
修改mysql5.7的编码由latin1为utf8
//查看默认编码
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.04 sec)
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
//退出mysql用户和,用root用户修改mysql配置文件
mysql> quit
Bye
用vi编辑my.cnf,在[mysqld]下添加 (最下面空白处)
character-set-server=utf8
collation-server=utf8_general_ci
[root@instance-8jlyr1f4 zhangliu]# vi /etc/my.cnf
按
I
进入INSERT后,按ESC
退出编辑模式,输入:wq
保存退出
重启mysql服务
systemctl restart mysqld.service
最后进入mysql用户,查看自己修改后的编码
[root@instance-8jlyr1f4 zhangliu]# mysql -p
Enter password:
mysql> show variables like "%character%";show variables like "%collation%";
若出现下图表明修改成功:
注意:默认安装的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)
至此,MySQL就安装结束。
若需远程连接MySQL还需开通服务器端口,开通端口请移步Linux开通端口