mysql> use mysql
mysql> create user 'test'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.12 sec)
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| test | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)
mysql>
mysql> grant all privileges on *.* to 'test3'@'%';
Query OK, 0 rows affected (0.12 sec)
mysql>
mysql> flush privileges; # 刷新权限
Query OK, 0 rows affected (0.04 sec)
mysql> create user 'test3'@'%' identified with mysql_native_password by '123456';
# 测试了一下,后面不用跟identified with mysql_native_password by '123456';加了反而报错
mysql> grant select,insert,update,delete on mydb.* to 'test3'@'%';
# 我测试了一下还没创建的数据库mydb1,没有报错,不过数据库mydb1没有自动创建,还不存在
mysql> grant select,insert,update,delete on mydb1.* to 'test3'@'%';
# 查看当前用户权限
mysql> show grants;
# 查看其它用户权限
mysql> show grants for test@localhost;
mysql> show grants for test@'%';
本地访问修改为所有人访问
mysql> create user 'test4'@localhost identified with mysql_native_password by '123456';
mysql> flush privileges;
# 将localhost修改为%
mysql> update user set host='%' where user='test4';
撤销用户权限
# 赋予所有权限
mysql> grant all on *.* to test@'%';
# 撤销已经赋予的权限
mysql> revoke all on *.* from test@localhost;
mysql> revoke all on *.* from test3@'%';
grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。
如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 "grant option",这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。
mysql> grant select on testdb.* to dba@localhost with grant option;
删除用户
mysql> drop user test3@'%';
mysql> drop user test@localhost;
远程登陆MySQL
# mysql -h ip -u root -p ,回车输入密码
mysql -h 192.168.92.145 -u my_user -p