MySQL的用戶管理
用戶名長度必須不超過16個字符
用戶名是大小寫敏感的
2# 創建用戶
語法:
```
(root@localhost)[(none)]> help create user
Name: ‘CREATE USER‘
Description:
Syntax:
CREATE USER user_specification [, user_specification] ...
user_specification:
user [ identified_option ]
auth_option: {
IDENTIFIED BY ‘auth_string‘
| IDENTIFIED BY PASSWORD ‘hash_string‘
| IDENTIFIED WITH auth_plugin AS ‘hash_string‘
}
The CREATE USER statement creates new MySQL accounts. An error occurs
if you try to create an account that already exists.
按照語法,最簡答的創建用戶的方法:
c(root@localhost)[mysql]> create user test1;
Query OK, 0 rows affected (0.00 sec)
(root@localhost)[mysql]> select user,host,password from user;
| user | host | password |
+-------+-----------+-------------------------------------------+
| root | localhost | *A0F874BC7F54EE086FCE60A37CE7887D8B31086B |
| test1 | % | |
2 rows in set (0.00 sec)
2 rows in set (0.00 sec)reate user test1;
這個時候其實密碼是空的,可以空密碼登錄的。
[mysql@mysql01 ~]$ mysql -S /data/mysqldata/3306/mysql.sock -utest1
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.31-log Source distribution
Copyright (c) 2000, 2016, 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.
(test1@localhost)[(none)]>
但是沒有任何權限:(USAGE這個權限,是代表廢物的意思!嗯,就是這樣)
(test1@localhost)[(none)]> show grants;
+-----------------------------------+
| Grants for test1@% |
+-----------------------------------+
| GRANT USAGE ON . TO ‘test1‘@‘%‘ |
+-----------------------------------+
1 row in set (0.00 sec)
3# 給用戶設置密碼:
命令
(root@localhost)[mysql]> help set password
Name: ‘SET PASSWORD‘
Description:
Syntax:
SET PASSWORD [FOR user] = password_option
password_option: {
PASSWORD(‘auth_string‘)
| OLD_PASSWORD(‘auth_string‘)
| ‘hash_string‘
}
給test1設置一個密碼:
(root@localhost)[mysql]> set password for test1=password(‘passwordtest‘);
Query OK, 0 rows affected (0.00 sec)
(root@localhost)[mysql]> select user,host,password from user where user=‘test1‘;
+-------+------+-------------------------------------------+
| user | host | password |
+-------+------+-------------------------------------------+
| test1 | % | *A76A397AE758994B641D5C456139B88F40610926 |
+-------+------+-------------------------------------------+
1 row in set (0.00 sec)
至於OLD_PASSWORD()函數,是為了兼容老版本的密碼而存在,古老的mysql4。
然而,set password for <user>=password(‘string‘); 這種修改方式已經被設置為要棄用,所以需要使用標準的修改密碼方式:
(root@localhost)[mysql]> alter user test1 identified by ‘password4test1‘;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘identified by ‘password4test1‘‘ at line 1
(root@localhost)[mysql]>
以上可見報錯了。原因是5.6還不支持這種密碼修改方式:
(root@localhost)[mysql]> help alter user;
Name: ‘ALTER USER‘
Description:
Syntax:
ALTER USER user_specification [, user_specification] ...
user_specification:
user PASSWORD EXPIRE
這裏只有一個子句,就是設置密碼過期
3# 賬號的密碼過期:
(root@localhost)[mysql]> alter user test1 password expire;
Query OK, 0 rows affected (0.00 sec)
(root@localhost)[mysql]> select user,host,password,password_expired from user;
+-------+-----------+-------------------------------------------+------------------+
| user | host | password | password_expired |
+-------+-----------+-------------------------------------------+------------------+
| root | localhost | A0F874BC7F54EE086FCE60A37CE7887D8B31086B | N |
| test1 | % | A76A397AE758994B641D5C456139B88F40610926 | Y |
+-------+-----------+-------------------------------------------+------------------+
2 rows in set (0.00 sec)
可以看到賬號密碼已經過期。
但是過期以後還是可以登錄,但是什麽都幹不了,會提示馬上更改密碼:
[mysql@mysql01 ~]$ mysql -S /data/mysqldata/3306/mysql.sock -utest1 -p‘passwordtest‘
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.31-log
Copyright (c) 2000, 2016, 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.
(test1@localhost)[(none)]> select 1
-> ;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
(test1@localhost)[(none)]>
#修改當前賬戶的密碼:
(test1@localhost)[(none)]> set password = password(‘password4test1‘);
Query OK, 0 rows affected (0.00 sec)
(test1@localhost)[(none)]>
#再次嘗試登錄,並做查詢測試
[mysql@mysql01 ~]$ mysql -S /data/mysqldata/3306/mysql.sock -utest1 -p‘password4test1‘
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.31-log Source distribution
Copyright (c) 2000, 2016, 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.
(test1@localhost)[(none)]> select 1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
#查詢成功,說明密碼更改成功。用管理賬號查詢use表查看賬號狀態:
(root@localhost)[mysql]> select user,host,password,password_expired from user;
+-------+-----------+-------------------------------------------+------------------+
| user | host | password | password_expired |
+-------+-----------+-------------------------------------------+------------------+
| root | localhost | A0F874BC7F54EE086FCE60A37CE7887D8B31086B | N |
| test1 | % | CFA887C680E792C2DCF622D56FB809E3F8BE63CC | N |
+-------+-----------+-------------------------------------------+------------------+
2 rows in set (0.00 sec)
4# 遠程登錄
在user表中,test1的host列值為%,代表可以從任意位置登錄mysql
[mysql@mysql01 ~]$ mysql -utest1 -p‘password4test1‘ -h 192.168.199.101 -P 3306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.31-log Source distribution
Copyright (c) 2000, 2016, 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.
([email protected])[(none)]>
5# 比較完整方式創建用戶
(root@localhost)[mysql]> create user test2@‘%‘ identified by ‘password4test2‘;
Query OK, 0 rows affected (0.00 sec)
(root@localhost)[mysql]> create user test2@‘192.168.199.101‘ identified by ‘test2local‘;
Query OK, 0 rows affected (0.00 sec)
(root@localhost)[mysql]> select user,host,password from user where user=‘test2‘;
+-------+-----------------+-------------------------------------------+
| user | host | password |
+-------+-----------------+-------------------------------------------+
| test2 | 192.168.199.101 | 74F386E8F5EEC7648BABDD0FCBA4524B97344856 |
| test2 | % | 5AB2E18AD9EE76F76E1C02E4DBF97BC7C3B4588B |
+-------+-----------------+-------------------------------------------+
2 rows in set (0.00 sec)
(root@localhost)[mysql]>
建立了兩個test2,這兩個test2是不同的,實際上應該說,用戶test2@‘192.168.199.101‘ 和用戶test2@‘%‘ 是兩個不同的用戶。
[mysql@mysql01 ~]$ mysql -utest2 -p‘test2local‘ -h 192.168.199.101 -P 3306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.6.31-log Source distribution
Copyright (c) 2000, 2016, 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.
([email protected])[(none)]>
[mysql@mysql01 ~]$ mysql -utest2 -S /data/mysqldata/3306/mysql.sock -p‘password4test2‘
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.6.31-log Source distribution
Copyright (c) 2000, 2016, 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.
(test2@localhost)[(none)]>
5# 修改密碼:
1,set password 方式:
(root@localhost)[mysql]> set password for test1=password(‘password4test1‘);
Query OK, 0 rows affected (0.00 sec)
2,直接update系統表user,這種方式需要刷新權限列表
(root@localhost)[mysql]> update user set password=password(‘password4test1‘) where user=‘test1‘;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
(root@localhost)[mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3,grant 方式
(root@localhost)[mysql]> grant usage on . to test1 identified by ‘password4test1‘;
Query OK, 0 rows affected (0.01 sec)
MySQL的用戶管理