MySQL的用戶管理

1# 建立用戶的一些限制和注意點
用戶名長度必須不超過16個字符
用戶名是大小寫敏感的mysql

2# 建立用戶
語法:sql

(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
| 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.

按照語法,最簡答的建立用戶的方法:ide

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# 給用戶設置密碼:
命令this

(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設置一個密碼:code

(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。server

然而,set password for <user>=password('string'); 這種修改方式已經被設置爲要棄用,因此須要使用標準的修改密碼方式:ip

(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還不支持這種密碼修改方式:ci

(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.

(test1@192.168.199.101)[(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.

(test2@192.168.199.101)[(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)
相關文章
相關標籤/搜索