第一步、前往mysql官網下載所需的版本
Mysql5.7的rpm包下載地址為https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
下載完成后就上傳的CentOS系統上。
第二步、解壓安裝
[root@youxi2 ~]# mkdir Mysql //創建一個專門的Mysql目錄 [root@youxi2 ~]# tar xf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar -C Mysql/ //將解壓的文件放到Mysql目錄下 [root@youxi2 ~]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel libaio libaio-devel net-tools //安裝依賴包
由於CentOS7開始自帶的數據庫是mariadb,所以需要卸載系統中的mariadb組件,才能安裝mysql的組件
[root@youxi2 ~]# rpm -qa | grep mariadb mariadb-libs-5.5.60-1.el7_5.x86_64 [root@youxi2 ~]# yum -y remove mariadb-libs
現在開始安裝mysql,由於依賴關系,所以順序是固定的。
[root@youxi2 ~]# rpm -ivh Mysql/mysql-community-common-5.7.16-1.el7.x86_64.rpm 警告:Mysql/mysql-community-common-5.7.16-1.el7.x86_64.rpm: 頭V3 DSA/SHA1 Signature, 密鑰 ID 5072e1f5: NOKEY 准備中... ################################# [100%] 正在升級/安裝... 1:mysql-community-common-5.7.16-1.e################################# [100%] [root@youxi2 ~]# rpm -ivh Mysql/mysql-community-libs-5.7.16-1.el7.x86_64.rpm 警告:Mysql/mysql-community-libs-5.7.16-1.el7.x86_64.rpm: 頭V3 DSA/SHA1 Signature, 密鑰 ID 5072e1f5: NOKEY 准備中... ################################# [100%] 正在升級/安裝... 1:mysql-community-libs-5.7.16-1.el7################################# [100%] [root@youxi2 ~]# rpm -ivh Mysql/mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm 警告:Mysql/mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm: 頭V3 DSA/SHA1 Signature, 密鑰 ID 5072e1f5: NOKEY 准備中... ################################# [100%] 正在升級/安裝... 1:mysql-community-libs-compat-5.7.1################################# [100%] [root@youxi2 ~]# rpm -ivh Mysql/mysql-community-client-5.7.16-1.el7.x86_64.rpm 警告:Mysql/mysql-community-client-5.7.16-1.el7.x86_64.rpm: 頭V3 DSA/SHA1 Signature, 密鑰 ID 5072e1f5: NOKEY 准備中... ################################# [100%] 正在升級/安裝... 1:mysql-community-client-5.7.16-1.e################################# [100%] [root@youxi2 ~]# rpm -ivh Mysql/mysql-community-server-5.7.16-1.el7.x86_64.rpm //之后安裝就成功了 警告:Mysql/mysql-community-server-5.7.16-1.el7.x86_64.rpm: 頭V3 DSA/SHA1 Signature, 密鑰 ID 5072e1f5: NOKEY 准備中... ################################# [100%] 正在升級/安裝... 1:mysql-community-server-5.7.16-1.e################################# [100%]
第三步、啟動mysql並設置開機自啟
[root@youxi2 ~]# systemctl start mysqld [root@youxi2 ~]# systemctl enable mysqld [root@youxi2 ~]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since 日 2019-06-02 12:11:34 CST; 45s ago Main PID: 7840 (mysqld) CGroup: /system.slice/mysqld.service └─7840 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 6月 02 12:11:26 youxi2 systemd[1]: Starting MySQL Server... 6月 02 12:11:34 youxi2 systemd[1]: Started MySQL Server.
第四步、獲取mysql臨時密碼,設置mysql的root用戶密碼
[root@youxi2 ~]# grep "password" /var/log/mysqld.log //前往日志文件查找臨時密碼 2019-06-02T04:11:28.935057Z 1 [Note] A temporary password is generated for root@localhost: zS+u&ro49wbo [root@youxi2 ~]# mysql -uroot -p"zS+u&ro49wbo" mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.16 Copyright (c) 2000, 2016, 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> alter user 'root'@'localhost' identified by '123456'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> alter user 'root'@'localhost' identified by 'root1234'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> alter user 'root'@'localhost' identified by 'root1234ABC'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> alter user 'root'@'localhost' identified by 'root1234ABCD!@#$'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> exit Bye
修改密碼出來使用“alter user 'root'@'localhost' identified by 'root1234ABCD!@#$';”,也可以使用“set password for root@localhost=password('root1234ABCD!@#$');”
第五步、測試
由於有特殊符號,必須用引號包裹密碼
[root@youxi2 ~]# mysql -u root -p'root1234ABCD!@#$' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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>
擴展:如果想要設置簡單密碼概如何操作?
有兩種方法,一種在mysql里使用命令修改,一種直接修改配置文件。
在mysql里使用命令修改的辦法:
[root@youxi2 ~]# mysql -u root -p'root1234ABCD!@#$' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> select @@validate_password_policy; //這個參數是密碼復雜程度 +----------------------------+ | @@validate_password_policy | +----------------------------+ | MEDIUM | +----------------------------+ 1 row in set (0.02 sec) mysql> select @@validate_password_length; //這個參數是密碼長度 +----------------------------+ | @@validate_password_length | +----------------------------+ | 8 | +----------------------------+ 1 row in set (0.00 sec) mysql> set global validate_password_policy=0; //global全局的 Query OK, 0 rows affected (0.02 sec) mysql> set global validate_password_length=1; Query OK, 0 rows affected (0.00 sec) mysql> set password for root@localhost=password('123'); ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> set password for root@localhost = password('1234'); //設置密碼 Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; //刷新 Query OK, 0 rows affected (0.00 sec) mysql> exit //退出 Bye [root@youxi2 ~]# mysql -uroot -p1234 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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>
說明:1.validate_password_policy復雜度級別:0表示密碼達到長度即可;1表示密碼需達到長度,還需有數字、大小寫字母(可以單一可以混合)以及特殊字符;2表示密碼需達到長度,還需數字、大小寫字母(可以單一可以混合)以及特殊字符字典文件。MEDIUM是中等,也就是1。
2.validate_password_length其實是一個動態的值,它的最小值等於validate_password_number_count+validate_password_special_char_count+(2*validate_password_mixed_case_count),而這三個參數分別對應密碼中數字、特殊字符、大小寫字母的最小數量。我操作時設置了validate_password_length=1,實際再次讀取validate_password_length的值是4。
mysql> select @@validate_password_length; +----------------------------+ | @@validate_password_length | +----------------------------+ | 4 | +----------------------------+ 1 row in set (0.00 sec)
直接修改配置文件的辦法:
[root@youxi2 ~]# vim /etc/my.cnf validate-password=OFF //在[mysqld]模塊內添加,將validate_password插件關閉 [root@youxi2 ~]# systemctl restart mysqld //重啟mysqld服務 [root@youxi2 ~]# mysql -uroot -p1234 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> set password for root@localhost=password('1'); //validate_password插件關閉后密碼長度只需大於等於1即可,復雜度沒有要求 Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; //刷新 Query OK, 0 rows affected (0.00 sec) mysql> exit //退出 Bye [root@youxi2 ~]# mysql -uroot -p1 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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>
注意:Mysql5.7是自帶validate_password插件,關閉后對密碼沒有復雜度要求,只需密碼長度大於等於1。
建議:/etc/my.cnf中將默認字符集設置為utf8,即添加一行character_set_server=utf8,然后重啟mysqld