创建MySQL用户并授权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[root@MasterDatabase ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 161391
Server version: 5.1.73-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| zabbix |
+--------------------+
4 rows in set (0.00 sec)

mysql> create user 'wordpress'@'%' identified by 'wordpress';
Query OK, 0 rows affected (0.00 sec)

mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)

mysql> grant all privileges on wordpress.* to 'wordpress'@'%';
Query OK, 0 rows affected (0.00 sec)


创建用户:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';

CREATE USER 'username'@'192.168.5.9' IDENTIFIED BY 'password';
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
CREATE USER 'username'@'%' IDENTIFIED BY '';
CREATE USER 'username'@'%';

授权:
GRANT privileges ON databasename.tablename TO 'username'@'host';

GRANT SELECT,INSERT ON DBname.tablename TO 'username'@'%';
GRANT ALL ON DBname.tablename TO 'username'@'%';
GRANT ALL ON DBname.* TO 'username'@'%';
GRANT ALL ON *.* TO 'username'@'%';
注意:使用以上命令授权的用户不能用来再给其他用户授权
如果想让该用户可以为其他用户授权,可以使用如下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;

设置/更改用户密码
SET PASSWORD FOR 'username'@'host'=PASSWORD('newpassword');
如果是修改当前登录的用户的密码,使用如下命令:
SET PASSWDORD=PASSWORD('newpassword')