Centos7.4 安裝Mysql5.7

yum下載網址爲:https://dev.mysql.com/downloads/repo/yum/
找到Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package,單擊後面的Download,
在新的頁面中單擊最下面的No thanks, just start my download.就可以下載到yum源了。
 
1)安裝MySQL YUM資源庫
[root@kevin ~]# yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
  
2)安裝MySQL 5.7
[root@kevin ~]# yum install -y mysql-community-server
  
3)啓動MySQL服務器和MySQL的自動啓動
[root@kevin ~]# systemctl start mysqld.service
[root@kevin ~]# systemctl enable mysqld.service
  
4)密碼問題
由於MySQL從5.7開始不允許首次安裝後使用空密碼進行登錄!爲了加強安全性,系統會隨機生成一個密碼以供管理員首次登錄使用,
這個密碼記錄在/var/log/mysqld.log文件中,使用下面的命令可以查看此密碼:
[root@kevin ~]# cat /var/log/mysqld.log|grep 'A temporary password'
2018-01-24T02:32:20.210903Z 1 [Note] A temporary password is generated for root@localhost: DOqInortw9/<
  
最後一行冒號後面的部分DOqInortw9/<就是初始密碼。
使用此密碼登錄MySQL:
[root@kevin ~]# mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.21
  
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
  
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  
使用隨機生產的密碼登錄到服務端後,必須馬上修改密碼,不然會報如下錯誤:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  
有兩種方法解決上面的報錯(如下的123456是修改後的密碼):
mysql> set password=password("123456");
或者
mysql> alter user 'root'@'localhost' identified by '123456';
  
刷新權限
mysql> flush privileges;
  
===============================================================================================
如果上面在執行set password=password("123456");命令後出現下面的報錯:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  
解決辦法:
這個與Mysql 密碼安全策略validate_password_policy的值有關,validate_password_policy可以取0、1、2三個值:
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
  
默認的數值是1,符合長度,且必須含有數字,小寫或大寫字母,特殊字符。
所以剛開始設置的密碼必須符合長度,且必須含有數字,小寫或大寫字母,特殊字符。
  
有時候,只是爲了自己測試,不想密碼設置得那麼複雜,譬如說,我只想設置root的密碼爲123456。
必須修改兩個全局參數:
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
  
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
  
修改上面兩個參數後,就可以解決這個報錯了。
=======================================================================================================
  
注意一點:
mysql5.7之後的數據庫裏mysql.user表裏已經沒有password這個字段了,password字段改成了authentication_string。
所以修改密碼的命令如下:
  
mysql> update mysql.user set authentication_string=password('kevin@123') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
  
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  
mysql>
  
=======================================================================================================
 
查看mysql版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.21    |
+-----------+
1 row in set (0.00 sec)
 
mysql>
 
=======================================================================================================
修改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.00 sec)
  
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.01 sec)
  
調整操作:
[root@kevin ~]# cat /etc/my.cnf
......
[mysqld]
......
character-set-server=utf8                //注意這個不能寫成default-character-set=utf8,否則會導致5.7版本mysql無法打開
  
[client]
default-character-set=utf8
  
[root@kevin~]# systemctl restart mysqld.service
[root@kevin~]# mysql -p
......
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
  
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
  
mysql>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章