轉自 https://blog.csdn.net/piaocoder/article/details/53704126
登錄MySQL
mysql -u root -p
添加新用戶
允許本地 IP 訪問 localhost, 127.0.0.1
create user 'test'@'localhost' identified by '123456';
允許外網 IP 訪問
create user 'test'@'%' identified by '123456';
刷新授權
flush privileges;
爲用戶創建數據庫
create database test DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
爲新用戶分配權限
授予用戶通過外網IP對於該數據庫的全部權限
grant all privileges on `testdb`.* to 'test'@'%' identified by '123456';
授予用戶在本地服務器對該數據庫的全部權限
grant all privileges on `testdb`.* to 'test'@'localhost' identified by '123456';
刷新權限
flush privileges;
退出 root 重新登錄
exit
用新帳號 test 重新登錄,由於使用的是 % 任意IP連接,所以需要指定外部訪問IP
mysql -u test -h 115.28.203.224 -p
在Ubuntu服務器下,MySQL默認是隻允許本地登錄,因此需要修改配置文件將地址綁定給註釋掉:
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1 #註釋掉這一行就可以遠程登錄了
不然會報如下錯誤:
ERROR 2003 (HY000): Can't connect to MySQL server on 'host' (111)