在本地連線遠端的MySQL資料庫(Linux)
阿新 • • 發佈:2019-01-27
嘗試了兩種在本地連線遠端 MySQL 資料庫的方式,一種是在本地使用 cmd 進行連線,還有一種就是使用 MySQL 的視覺化工具 Navicate for MySQL 進行連線
一、對遠端 MySQL 進行授權
由於遠端的 MySQL 預設是不允許從遠端訪問他的,因此我們需要先進行授權,
1. 使用的一種方法是從任何主機都可以連線到 MySQL 伺服器,方法如下:
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
我使用的就是這一種,親測可用
2. 如果你想允許使用者myuser從ip為192.168.1.6的主機連線到mysql伺服器,並使用mypassword作為密碼
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
3. 如果你想允許使用者myuser從ip為192.168.1.6的主機連線到mysql伺服器的dk資料庫,並使用mypassword作為密碼
GRANT ALL PRIVILEGES ON dk.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; FLUSH PRIVILEGES;
FLUSH PRIVILEGES
:重新整理許可權
二、使用 cmd 連線
MySQL 的命令列登陸格式為:
mysql -h主機地址 -u使用者名稱-p使用者密碼
主機地址:如果連線的是本地資料庫,則不用填;如果是遠端資料庫,則填寫伺服器的 ip
**注:**如直接執行此命令需要配置環境變數,否則需要先進入到 MySQL 的安裝路徑下然後在執行命令,配置環境變數過程不在贅述
接下來輸入引數,登陸成功結果如下:
C:\Users\luwen> mysql -h120.78.222.54 -uroot -p Enter password: **** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 47 Server version: 5.7.22 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 | | bank | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.04 sec)
大功告成?
**注:**如果出現錯誤,很有可能就是因為你沒有設定許可權,因此一定要設定許可權!!!
三、使用 Navicate for MySQL 連線
點選連線,設定相關引數
操作成功