登陸MySQLmysql
mysql -u root -p
添加新用戶
容許本地 IP 訪問 localhost, 127.0.0.1sql
create user 'test'@'localhost' identified with 加密方式(mysql_native_password) by '123456';數據庫
容許外網 IP 訪問服務器
create user 'tst'@'%' identified by '123456';less
刷新受權
flush privileges;ide
爲用戶建立數據庫
create database test DEFAULT CHARSET utf8 COLLATE utf8_general_ci;加密
爲新用戶分配權限
授予用戶經過外網IP對於該數據庫的所有權限.net
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 -pcode
在Ubuntu服務器下,MySQL默認是隻容許本地登陸,所以須要修改配置文件將地址綁定給註釋掉:server
# 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 #註釋掉這一行就能夠遠程登陸了否則會報以下錯誤: