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開通端口