linux(centos7.4)安裝mysql5.7

安裝方式:

yum方式安裝

安裝步驟:

一、安裝MySQL YUM資源庫

 yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

二、安裝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-01-24T09:36:20.210903Z 1 [Note] A temporary password is generated for root@localhost: DOaIworrw5/<

最後一行冒號後面的部分DOaIworrw5/<就是初始密碼。

使用此密碼登錄MySQL:

[root@localhost opt]# mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26

Copyright (c) 2000, 2019, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

五、修改密碼

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('sadevqq@1234') 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後執行下面語句

update mysql.user set authentication_string=password('123456') where user='root';
flush privileges;

六、修改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]
......
//注意這個不能寫成default-character-set=utf8,否則會導致5.7版本mysql無法打開
character-set-server=utf8 

[client]
default-character-set=utf8

//重啓mysql
[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>

七、開放遠程登錄

navicat連接報錯:Host 'XXX' is not allowed to connect to this MySQL server

如何開啓MySQL的遠程登陸
1、首先以 root 帳戶登陸 MySQL

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)

mysql> grant all PRIVILEGES on test_db.* to root@'192.168.1.101'  identified by '123456';

上面的語句表示將 test_db 數據庫的所有權限授權給 root 這個用戶,允許 root 用戶在 192.168.1.101 這個 IP 進行遠程登陸,並設置 root 用戶的密碼爲 123456 。

下面逐一分析所有的參數:

all PRIVILEGES 表示賦予所有的權限給指定用戶,這裏也可以替換爲賦予某一具體的權限,例如select,insert,update,delete,create,drop 等,具體權限間用“,”半角逗號分隔。

test_db.* 表示上面的權限是針對於哪個表的,test_db指的是數據庫,後面的 * 表示對於所有的表,
由此可以推理出:對於全部數據庫的全部表授權爲“.”,對於某一數據庫的全部表授權爲“數據庫名.*”,對於某一數據庫的某一表授權爲“數據庫名.表名”。

root 表示你要給哪個用戶授權,這個用戶可以是存在的用戶,也可以是不存在的用戶。

192.168.1.101 表示允許遠程連接的 IP 地址,如果想不限制鏈接的 IP 則設置爲“%”即可。

123456 爲用戶的密碼。

執行了上面的語句後,再執行下面的語句,方可立即生效。

   flush privileges; 

最終我們可以知道,如果要完全開放遠程登錄則只需要這樣執行命令即可

//對全部數據庫的全部表授權、不限制鏈接的 IP、修改密碼爲123456
grant all PRIVILEGES on *.* to root@'%'  identified by '123456';
flush privileges; 

如果該文章有幫助到您,就留言點個贊吧!您的支持與肯定是我持續更新最大的動力。

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