天天看點

MySQL8遠端授權

原文連結:http://kuaibao.qq.com/s/20180420G1W8MG00?refer=cp_1026

1:MySql8的安裝與使用

注意:以下指令都在MYSQL_HOME/bin目錄下執行。

待設定了PATH=MYSQL_HOME/bin之後,就可以在任意的目錄下執行以下指令了。

Mysql8的新特性:

https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-4.html

Mysql8的文檔:

https://dev.mysql.com/doc/refman/8.0/en/

步1:解壓并初始化資料庫執行指令

mysqld –initialize

初始化完成以後,會在mysql解壓的目錄下,出現data目錄。

在data目錄下,找到:*.err的檔案。打開,檢視預設root使用者的密碼:

A temporary password is generated for [email protected]: a9&2uyvjfw,O

上面的:a9&2uyvjfw,O就是預設root使用者的密碼。

步2:啟動mysql資料庫[以測試的形式啟動]

可以使用mysqld –console啟動mysql資料庫,等待都配置完成以後再安裝成mysql的服務。

指令:

mysqld –console

現在啟動mysql資料庫。在測試狀态下可以使用mysqld –console即在指令行啟動。

D:\programfiles\mysql-8.0.11-winx64\bin>mysqld –console

2018-04-20T10:24:50.439301Z 0 [System] [MY-010116] [Server] D:\programfiles\mysq

l-8.0.11-winx64\bin\mysqld.exe (mysqld 8.0.11) starting as process 2420

2018-04-20T10:24:52.186504Z 0 [Warning] [MY-010068] [Server] CA certificate ca.p

em is self signed.

2018-04-20T10:24:52.233305Z 0 [System] [MY-010931] [Server] D:\programfiles\mysq

l-8.0.11-winx64\bin\mysqld.exe: ready for connections. Version: ‘8.0.11’ socket
” port: 3306 MySQL Community Server - GPL.

步3:登入mysql資料庫

再次打開一個新的指令行視窗,然後輸入:mysql -uroot -p登入:

mysql -uroot -p

Password:[請在這兒輸入上面得到的mysql root使用者的預設密碼]

以下是登入過程:

D:\programfiles\mysql-8.0.11-winx64\bin>mysql -uroot -p

Enter password: **

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.11

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.

步4:修改密碼

預設的情況下,root使用者使用臨時密碼登入,不能做任何的操作。 這一點與mysql5.7版本一樣。是以,必須要先修改密碼才可以使用mysql8。現在讓我們來修改root使用者的的密碼:

mysql> alter user ‘root’@’localhost’ identified by ‘1234’;

Query OK, 0 rows affected (0.12 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

在重新整理權限以後,就可以執行一些操作了,如顯示資料庫清單:

mysql> show databases;

+——————–+

| Database |

+——————–+

| information_schema |

| mysql |

| performance_schema |

| sys |

+——————–+

4 rows in set (0.01 sec)

步5:建立一個新的使用者并授權

注意:

在MySql5時,可以直接使用:

Grant all privileges on . to‘someBody’@’SomeIP’identified by‘somePassword’[with grant option];

的文法,直接建立一個新的使用者且授權,但在mysql8中,上面的語句已經不再适用。

重點内容

mysql8中必須要先建立一個使用者,然後再進行授權,可以說是更加的規範了。

見官方說明:

重點内容

https://dev.mysql.com/doc/refman/8.0/en/adding-users.html

以下是建立和授權的指令:[更請參考官網的示例]

mysql> create user ‘wj’@’%’ identified by ‘1234’;

Query OK, 0 rows affected (0.04 sec)

mysql> grant all privileges on . to ‘wj’@’%’ with grant option;

Query OK, 0 rows affected (0.11 sec)

步6:使用新建立的使用者登入測試

mysql -uwj -p -h192.168.2.104 -P3306

步7:安裝成mysql的服務

在上面的測試都通過以後,現在就可以全部退出,再說一遍,全部退出。然後在MYSQL_HOME/bin目錄下,執行mysqld –install,将mysql安裝成服務。

D:\programfiles\mysql-8.0.11-winx64\bin>mysqld –install

Service successfully installed.

然後,使用net start啟動mysql或是使用net stop關閉mysql的服務:

D:\programfiles\mysql-8.0.11-winx64\bin>net start mysql

MySQL服務正在啟動.

MySQL服務已經啟動成功。

啟動成功以後,就可以關閉這個指令行視窗了。因為mysql已經做為服務啟動了。

步8:現在配置mysql的環境變量

配置以下環境變量:

PATH=D:\programfiles\mysql-8.0.11-winx64\bin

測試登入:

C:\Users\Administrator>mysql -uroot -p

Enter password: **

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.11 MySQL Community Server - GPL

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;

+——————–+

| Database |

+——————–+

| information_schema |

| mysql |

更多新特性等我看完以下DOC以後再給大家一一總結。無論如何,學好SQL非常重要,無論是在垂直項目還是在大資料項目中,SQL都顯得非常重要。

其他:

最主要的是mysql8登入的密碼協定發生了變更,使用mysql指令行用戶端完全可以登入,但是使用navicat或是sqlyog登入都不成功,見以下錯誤。顯示的意思為“認證協定“錯誤。

見官方文檔:

https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-4.html#mysqld-8-0-4-security

通過讀取上面的文檔我們可知:

1:mysql已經将之前的mysql_native_password認證,修改成了caching_sha2_password認證方式。

是以,使用類似于navicat或是sqlyog這些用戶端時,預設使用還是mysql_native_password認證方式,是以即使輸入正确的使用者和密碼依然登入不成功。

解決方式1:

在建立使用者時,指定使用mysql_native_password認證方式:

mysql> create user ‘Jack’@’%’ identified with mysql_native_password by ‘1234’;

Query OK, 0 rows affected (0.04 sec)

mysql> grant all privileges on . to ‘Jack’@’%’;

Query OK, 0 rows affected (0.13 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

官方示例:

CREATEUSER’nativeuser’@’localhost’IDENTIFIEDWITHmysql_native_passwordBY’password’;

是以,為了更好的解決登入問題,可以以下處理方式:

建立一個root使用者,密碼認證方式為:mysql_native_password,且root使用者可以在任意用戶端登入,即[email protected]%:

mysql> create user ‘root’@’%’ identified with mysql_native_password by ‘1234’;

Query OK, 0 rows affected (0.11 sec)

給這個[email protected]%進行授權:

mysql> grant all privileges on . to ‘root’@’%’ with grant option;

Query OK, 0 rows affected (0.06 sec)

重新整理權限

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

删除[email protected]使用者:

mysql> drop user ‘root’@’localhost’;

Query OK, 0 rows affected (0.06 sec)

重新整理權限:

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

現在navicat就可以正常登入使用了:

新建立連接配接,輸入使用者名和密碼:

檢視資料庫資訊:

繼續閱讀