1. 程式人生 > 其它 >mariadb資料庫使用者管理(建立、賦權、)

mariadb資料庫使用者管理(建立、賦權、)

資料庫檢視當前使用者 select user();

MariaDB [(none)]> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

資料庫新建使用者 (--建立了一個名為:test 密碼為:1234 的使用者)

MariaDB [(none)]> create user 'test'@'localhost' identified by '1234';
Query OK, 0 rows affected (0.00 sec)

 

查詢使用者

MariaDB [zabbix]> select user,host from mysql.user;
+--------+-----------+
| user   | host      |
+--------+-----------+
| root   | 127.0.0.1 |
| root   | ::1       |
| root   | localhost |
| test   | localhost |
| zabbix | localhost |
+--------+-----------+
5 rows in set (0.00 sec)

修改使用者密碼

方法1,密碼實時更新;修改使用者“test”的密碼為“5678”

MariaDB [mysql]> set password for test@localhost =password('5678');                                     
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit
Bye

[root@192 ~]# mysql -utest -p5678
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 271
Server version: 10.2.36-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> select user();
+----------------+
| user() |
+----------------+
| test@localhost |
+----------------+
1 row in set (0.00 sec)

 方法2 ,需要重新整理;修改使用者“test”的密碼為“1234”

MariaDB [(none)]> update mysql.user set password =password('root') where user='test';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [(none)]> flush privileges;  重新整理許可權
Query OK, 0 rows affected (0.00 sec)

  方法3,命令列修改 mysqladmin-u使用者名稱 -p舊密碼 password 新密碼

[root@192 ~]# mysqladmin -uroot -p1 password root
[root@192 ~]# mysql -uroot -proot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 95
Server version: 10.2.36-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

分配使用者許可權

授予使用者test通過外網IP對所有資料庫所有表的全部許可權

MariaDB [(none)]> grant all privileges on *.* to 'test'@'%' identified by '1234'; 
Query OK, 0 rows affected (0.00 sec)

 注意:修改完許可權以後 一定要重新整理服務,或者重啟服務,重新整理服務用:flush privileges; 

檢視使用者許可權 show grants for test;

MariaDB [(none)]> show grants for test;
+--------------------------------------------------------------------------------------------------------------+
| Grants for test@%                                                                                            |
+--------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' IDENTIFIED BY PASSWORD '*A4B6157319038724E3560894F7F932C8886EBFCF' |
+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

  

刪除使用者 drop user test;

MariaDB [(none)]> drop user test;
Query OK, 0 rows affected (0.00 sec)