一. 创立用户
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
username:你将创立的用户名
host:指定该用户在哪个主机上能够登陆,从本地登录填 localhost,任意主机登陆填通配符 %
password:登陆密码,明码能够为空,如果为空则该用户能够不须要明码也可登陆
例如:
CREATE USER 'one'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'one'@'192.168.1.101' IDENDIFIED BY '123456';
CREATE USER 'one'@'%' IDENTIFIED BY '123456';
CREATE USER 'one'@'%' IDENTIFIED BY '';
CREATE USER 'one'@'%';
二. 受权:
GRANT privileges ON databasename.tablename TO 'username'@'host'
阐明:
privileges:用户的操作权限,如 SELECT,INSERT,UPDATE 等,如果要授予所的权限则应用 ALL
databasename:数据库名,如果授予整个数据库权限填 databasename.*
tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用 * 示意,如 *.*
例子:
GRANT SELECT, INSERT ON test.user TO 'one'@'%';
GRANT SELECT, INSERT ON test.*TO 'one'@'%';
GRANT ALL ON *.* TO 'one'@'%';
用以上命令受权的用户不能给其它用户受权,如果想让该用户能够受权,用以下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
三. 设置与更改用户明码
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
如果是以后登陆用户用:
SET PASSWORD = PASSWORD("newpassword");
例子:
SET PASSWORD FOR 'one'@'%' = PASSWORD("123456");
四. 撤销用户权限
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
相干阐明:
privilege, databasename, tablename:同受权局部
例子:
REVOKE SELECT ON *.* FROM 'one'@'%';
留神:
如果你在给用户 ’one’@’%’ 受权的时候是这样的(或相似的):
GRANT SELECT ON test.user TO 'one'@'%',
则在应用
REVOKE SELECT ON *.* FROM 'one'@'%';
命令并不能撤销该用户对 test 数据库中 user 表的 SELECT 操作。
相同,如果受权应用的是
GRANT SELECT ON *.* TO 'one'@'%';
则
REVOKE SELECT ON test.user FROM 'one'@'%';
命令也不能撤销该用户对 test 数据库中 user 表的 Select 权限。
具体信息能够用如下查看。
SHOW GRANTS FOR 'one'@'%';
五. 删除用户
DROP USER 'username'@'host';
六. 遇到的问题
创立实现后用 Navicat 创立表遇到了报错:
Access denied; you need (at least one of) the PROCESS privilege(s)
依据提醒是短少 PROCESS 权限,赋予后问题解决
mysql> grant process on MyDB.* to test;
ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
第一次授予这样的权限,谬误起因是 process 权限是一个全局权限,不能够指定在某一个库上(集体测试库为 MyDB),所以,把受权语句更改为如下即可:
mysql> grant process on *.* to test;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
如果不给领有授予 PROESS 权限,show processlist 命令只能看到以后用户的线程,而授予了 PROCESS 权限后,应用 show processlist 就能看到所有用户的线程。官网文档的介绍如下:
SHOW PROCESSLIST shows you which threads are running.
You can also get this information from the INFORMATION_SCHEMA PROCESSLIST table
or the mysqladmin processlist command.
If you have the PROCESS privilege, you can see all threads. Otherwise,
you can see only your own threads (that is, threads associated with the
MySQL account that you are using). If you do not use the FULL keyword,
only the first 100 characters of each statement are shown in the Info field.