当前使用的user及host:
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql5以前版本直接使用 insert 向 mysql 表中插入mysql用户了,mysql5之后不可以这样操作
mysql> insert into mysql.user(host,user,password) values('localhost','test_user',password('123123'));
error 1062 (23000): duplicate entry 'localhost-test_user' for key 'primary'
增加用户 {授予用户指定数据表权限 [使用 grant 命令 对用户进行相应授权]}
mysql> grant all privileges on table1.* to
'test_user'@'localhost' identified by '123123' with grant option;
query ok, 0 rows affected (0.02 sec)
identified by 指定用户的登录密码
all privileges 是表示所有权限,也可以使用 select、update 等权限
*.\ 中前面的*号用来指定数据库名,后面的*号用来指定表名
to 表示将权限赋予某个用户
on 用来指定权限针对哪些库和表
'test_user'@'localhost' 表示test_user用户,@后面接限制的主机,可以是ip、ip段、域名以及%,%表示任何地方
with grant option 这个选项表示该用户可以将自己拥有的权限授权给别人
需要刷新系统权限表[flush privilege] 该用户才能生效登录
mysql> flush privileges;
删除用户
mysql> drop user 'test_user'@'localhost';
查看当前用户的权限
mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| grant all privileges on *.* to 'root'@'localhost' identified by password '\*e56a114692fe0de073f9a1dd68a00eeb9703f3f1' with grant option |
| grant proxy on ''@'' to 'root'@'localhost' with grant option |
+----------------------------------------------------------------------------------------------------------------------------------------+
查看某个用户的权限
mysql> show grants for 'test_user'@'localhost'
+------------------------------------------------------------------------------------------------------------+
| grants for test_user@localhost |
+------------------------------------------------------------------------------------------------------------+
| grant usage on *.* to 'test_user'@'localhost' identified by password '\*e56a114692fe0de073f9a1dd68a00eeb9703f3f1' |
| grant all privileges on table1.* to 'test_user'@'localhost' with grant option |
+------------------------------------------------------------------------------------------------------------+
对账户重命名
mysql> rename user 'test_user'@'localhost' to 'bb'@'localhost';
修改密码
1.用set password命令
mysql> set password for 'test_user'@'localhost' = password('123456');
2.用 mysqladmin [进入bin目录]
备注:{格式: mysqladmin -u用户名 -p旧密码 password 新密码]
/usr/bin$ mysqladmin -utest_user -p123456 password 123123
mysqladmin: can't turn off logging; error: 'access denied; you need (at least one of) the super privilege(s) for this operation'
3.用 update 直接编辑 user 表
mysql> use mysql
reading table information for completion of table and column names
you can turn off this feature to get a quicker startup with -a
database changed
mysql> update user set password = password('123123') where user = 'test_user';
query ok, 1 row affected (0.04 sec)
rows matched: 1 changed: 1 warnings: 0