1、通过cmd进入MySQL的bin目录下:
2、通过root账号进入数据库
mysql -u root -p123456
3、创建新用户,并授权该用户可以操作的数据库和表
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;
4、如何修改用户密码
- 使用root账号登陆
- 使用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 = '%';
5、如何撤销用户权限
revoke all on 数据库名.表名 from '用户名'@'主机名';
6、删除用户
drop user '用户名'@’主机名‘;