当前使用的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
推荐教程:《MySQL教程》