MySQL创建用户与授权方法

1.创建用户:

命令:create user 'username'@'host' identified by 'password'; 

说明:username - 用户名, host - 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%. password - 登陆密码 

mysql> create user 'wangchengjun'@'%' identified by '123456';

Query OK, 0 rows affected (0.01 sec)

mysql>flush privileges;  #刷新权限

mysql> select user,host from user;

+--------------+-----------+

| user         | host      |

+--------------+-----------+

| wangchengjun | %         |

| root         | 127.0.0.1 |

| root         | localhost |

| root         | wang      |

+--------------+-----------+

4 rows in set (0.00 sec)

2.授权

命令:grant privileges on databasename.tablename to 'username'@'host'

说明: privileges - 用户的操作权限,如SELECT , INSERT , UPDATE 等(详细列表见该文最后面).如果要授予所的权限则使用ALL.;databasename - 数据库名,tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*. 

mysql> grant all on wordpress.* to 'wangchengjun'@'%'; 

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges; 

Query OK, 0 rows affected (0.00 sec)

具体信息可以用命令show grants for 'wangchengjun'@'%'; 查看.

3.设置与更改用户密码

root账户设置密码:mysqladmin -uroot password 'yourpassword'

命令:set password for 'username'@'host' = password('newpassword');

mysql> set password for 'wangchengjun'@'%' = password("12345");

Query OK, 0 rows affected (0.00 sec)

4.撤销用户权限 

命令: prevoke privilege on databasename.tablename from 'username'@'host';

prevoke all on wordpress.* from "wangchengjun"@"%";

5.删除用户

命令: drop  user 'username'@'host'; 

drop user 'wangchengjun'@'%';

6.用户远程登录

mysql -uuser -ppassword -hhost -P3306

mysql -uwangcheng -p123456 -h192.168.16.99 -P3306



附表:在MySQL中的操作权限

ALTER Allows use of ALTER TABLE.

ALTER ROUTINE Alters or drops stored routines.

CREATE Allows use of CREATE TABLE.

CREATE ROUTINE Creates stored routines.

CREATE TEMPORARY TABLE Allows use of CREATE TEMPORARY TABLE.

CREATE USER Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES.

CREATE VIEW Allows use of CREATE VIEW.

DELETE Allows use of DELETE.

DROP Allows use of DROP TABLE.

EXECUTE Allows the user to run stored routines.

FILE Allows use of SELECT... INTO OUTFILE and LOAD DATA INFILE.

INDEX Allows use of CREATE INDEX and DROP INDEX.

INSERT Allows use of INSERT.

LOCK TABLES Allows use of LOCK TABLES on tables for which the user also has SELECT privileges.

PROCESS Allows use of SHOW FULL PROCESSLIST.

RELOAD Allows use of FLUSH.

REPLICATION Allows the user to ask where slave or master

CLIENT servers are.

REPLICATION SLAVE Needed for replication slaves.

SELECT Allows use of SELECT.

SHOW DATABASES Allows use of SHOW DATABASES.

SHOW VIEW Allows use of SHOW CREATE VIEW.

SHUTDOWN Allows use of mysqladmin shutdown.

SUPER Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached.

UPDATE Allows use of UPDATE.

USAGE Allows connection without any specific privileges.


你可能感兴趣的:(mysql,授权,创建用户)