命令列如何遠端連線MySQL資料庫
新使用MySQL,說起來是個簡單的事情,但是卻費了些周折:
1、登陸伺服器端,進入命令列,windows cmd;
2、設定使用者、密碼讓指定的IP訪問:mysql -u root -p 或安裝的快捷方式進入:MySQL Command Line Client,使用grant命令:
grant 許可權1,許可權2,…許可權n on 資料庫名稱.表名稱 to 使用者名稱@使用者地址 identified by '連線口令';
例子:
mysql>grant select,insert,update,delete,create,drop on mydb.mytable to [email protected]
給來自192.168.1.88的使用者lzpddd分配可對資料庫 mydb的mytable表進行select,insert,update,delete,create,drop等操作的許可權,並設定口令為'mypassword';
mysql>grant all privileges on *.* to [email protected] identified by 'mypassword';
給來自192.168.1.88的使用者lzpddd分配可對所有資料庫的所有表進行所有操作的許可權限,並設定口令為'mypassword';
mysql>grant all privileges on *.* to [email protected]'%' identified by 'mypassword';
給來自任何IP地址的使用者lzpddd分配可對所有資料庫的所有表進行所有操作的許可權限,並設定口令為'mypassword';
3、關閉伺服器端防火牆或者開放3306埠(很重要);
4、客戶端連線:
進入命令列,windows cmd,連線:
mysql -u 使用者名稱 -p密碼 -h 伺服器IP地址 -P 伺服器端MySQL埠號 -D 資料庫名
注意:
(1)伺服器埠標誌-P一定要大些以區別於使用者-p,如果直接連線資料庫標誌-D也要大寫;
(2)如果要直接輸入密碼-p後面不能留有空格如-pmypassword;
(3)命令結束段沒有';'分號。
例子:
mysql -u lzpddd -pmypassword -h 192.168.1.88 -P 3306 -D mydb
注意: ubuntu需要修改檔案$sudo gedit /etc/mysql/my.cnf
skip-networking
=> # skip-networking
或者
bind-address=127.0.0.1
=> bind-address= 你機器的IP