1.通過root賬號登陸數據庫
mysql -uroot -p
然后輸入root賬號密碼
2.創建新用戶,並授權該用戶可以操作的數據庫和表
grant all privileges on 數據庫名.表名 to '用戶名'@'主機名' identified by '密碼' with grant option;
flush privileges;
數據庫名:如果為*,表示所有數據庫
表名:如果為*,表示所有表
*.*表示root權限,即滿權限
主機名:localhost表示僅允許本地連接,%表示本地和遠程均可連接
flush privileges;表示刷新權限,使授權生效
所以允許遠程連接的時候可以使用:
grant all privileges on *.* to 'root'@'%' identified by 'root賬號密碼' with grant option;
比如我們新建test用戶,授予該用戶的權限是僅能操作test_database數據庫,密碼‘123’
grant all privileges on test_database.* to 'test'@'%' identified by '123' with grant option;
3.如何修改用戶密碼
- root賬號登陸
mysql -u root -p
- 使用mysql數據庫
use mysql;
- 查看user表
select host,user,authentication_string from user;
結果如下:
+-----------+------------------+-------------------------------------------+ | host | user | authentication_string | +-----------+------------------+-------------------------------------------+ | localhost | root | *6C2FC1038AB41E3B2B6D85B409E0F2B9C11BC8D3 | | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | debian-sys-maint | *E73AA584982C771D0F8B40367F92049530E668D4 | | % | root | *6C2FC1038AB41E3B2B6D85B409E0F2B9C11BC8D3 | | % | test | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | +-----------+------------------+-------------------------------------------+
- 修改用戶密碼:
update user set authentication_string = password(‘新密碼’) where user = '用戶名' and host = '主機名';
password()為mysql自身的一個加密函數
以修改test用戶密碼為'456'為例
update user set authentication_string = password('456') where user = 'test' and host = '%';
4.如何撤銷用戶權限
revoke all on 數據庫名.表名 from '用戶名'@'主機名';
5.如何刪除用戶
drop user '用戶名'@’主機名‘;