MySQL添加新用戶、為用戶創建數據庫、為新用戶分配權限


登錄MySQL

mysql -u root -p
添加新用戶
允許本地 IP 訪問 localhost, 127.0.0.1

create user 'test'@'localhost' identified with 加密方式(mysql_native_password) by '123456';


允許外網 IP 訪問

create user 'tst'@'%' 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)
---------------------
作者:piaocoder
來源:CSDN
原文:https://blog.csdn.net/piaocoder/article/details/53704126
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM