溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mysql初始化、增刪改查用戶、授權(quán)

發(fā)布時(shí)間:2020-06-26 11:34:32 來(lái)源:網(wǎng)絡(luò) 閱讀:704 作者:liximkuan 欄目:MySQL數(shù)據(jù)庫(kù)

1. 數(shù)據(jù)庫(kù)安全初始化

[root@elasticsearch my.cnf.d]# mysql_secure_installation        #安全初始化命令

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):               #輸入mysql的root賬戶默認(rèn)密碼(默認(rèn)為空)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y                      #是否設(shè)置root密碼
New password:                                   #為root用戶輸入一個(gè)新密碼
Re-enter new password:                          #再次輸入密碼
Password updated successfully!                  #密碼更新成功
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y                 #是否刪除匿名用戶
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y           #是否允許root用戶遠(yuǎn)程登錄
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]    #是否刪除test數(shù)據(jù)庫(kù)
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]              #是否刷新以上操作,使其立即生效
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
  • 以上操作完成后,命令行直接輸入mysql無(wú)法登錄數(shù)據(jù)庫(kù),使用本機(jī)外網(wǎng)地址也無(wú)法登錄。
[root@elasticsearch my.cnf.d]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@elasticsearch my.cnf.d]# mysql -uroot -h292.168.0.194 -p
Enter password: 
ERROR 1130 (HY000): Host 'node1' is not allowed to connect to this MariaDB server
[root@elasticsearch my.cnf.d]# mysql -uroot -h227.0.0.1 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.60-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.

[root@elasticsearch my.cnf.d]# mysql -uroot -hlocalhost -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.60-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)]> show databases;
  • 查看當(dāng)前授權(quán)的用戶和地址可見root僅授權(quán)了localhost和127.0.0.1可登錄
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select User,Host from user;
+------+-----------+
| User | Host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)

MariaDB [mysql]> 

2. 忘記管理員密碼的解決辦法:

  1. 啟動(dòng)mysql前,編輯/etc/my.cnf,添加skip-grant-tables和skip-networking;
[mysqld]
skip-grant-tables
skip-networking
datadir=/var/lib/mysql
  1. 通過UPDATE命令修改管理員密碼;
[root@elasticsearch ~]# systemctl start mariadb
[root@elasticsearch ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-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)]> update mysql.user set authentication_string=password('centos') where user='root' and Host = 'localhost';
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)

MariaDB [(none)]> exit
Bye
  1. 刪除/etc/my.cnf中添加的內(nèi)容,以正常方式啟動(dòng)mysqld進(jìn)程;
[root@elasticsearch ~]# mysql -uroot -hlocalhost -pcentos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-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)]> 

3. 增、刪、改、查用戶

  1. 查看用戶:
  • mysql中用戶表在mysql.user中
  • 查看用戶示例:
MariaDB [(none)]> SELECT User,Host FROM mysql.user;
+-------+-------------+
| User  | Host        |
+-------+-------------+
| root  | 127.0.0.1   |
| root  | ::1         |
| root  | localhost   |
+-------+-------------+
6 rows in set (0.01 sec)
  1. 添加用戶
    • 格式:可一次創(chuàng)建多個(gè)用戶
CREATE USER  'user'@'host' [IDENTIFIED BY [PASSWORD] 'password'] [,'user'@'host' [IDENTIFIED BY [PASSWORD] 'password']...]
  • 例:
# 單條命令創(chuàng)建一個(gè)用戶:
MariaDB [(none)]> CREATE USER 'lxk'@'localhost' IDENTIFIED BY PASSWORD 'linux.centos.com';
Query OK, 0 rows affected (0.00 sec)
# 以逗號(hào)為分隔,單條命令創(chuàng)建兩個(gè)用戶:
MariaDB [(none)]> CREATE USER 'test0'@'192.168.1.%' IDENTIFIED BY 'maria.centos.com','test1'@'192.168.1.%' IDENTIFIED BY 'maria.centos.com';
Query OK, 0 rows affected (0.00 sec)
  1. 重命名用戶:
    • 格式:
    • RENAME USER old_user TO new_user[, old_user TO new_user] ...
  • 例:(創(chuàng)建用戶時(shí)加了授權(quán)地址,修改時(shí)也需加上授權(quán)地址)
#查看當(dāng)前用戶:
MariaDB [(none)]> SELECT User,Host FROM mysql.user;
+-------+-------------+
| User  | Host        |
+-------+-------------+
| root  | 127.0.0.1   |
| test0 | 192.168.1.% |
| test1 | 192.168.1.% |
| root  | ::1         |
| lxk   | localhost   |
| root  | localhost   |
+-------+-------------+
6 rows in set (0.01 sec)

MariaDB [(none)]> RENAME USER 'test1'@'192.168.1.%' TO 'test001'@'192.168.1.%';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SELECT User,Host FROM mysql.user;
+---------+-------------+
| User    | Host        |
+---------+-------------+
| root    | 127.0.0.1   |
| test0   | 192.168.1.% |
| test001 | 192.168.1.% |
  1. 刪除用戶:
    • 格式:
    • DROP USER 'user'@'host' [, 'user'@'host'] ...
    • 例:
MariaDB [(none)]> DROP USER 'test001'@'192.168.1.%';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SELECT User,Host FROM mysql.user;
+-------+------------+
| User  | Host       |
+-------+------------+
| root  | 127.0.0.1  |
| test0 | 192.168.1.%|
| root  | ::1        |
| lxk   | localhost  |
| root  | localhost  |
+-------+------------+
5 rows in set (0.00 sec)
  1. 重新加載授權(quán)表:
    • 作用:有時(shí)操作并不會(huì)馬上寫到磁盤上,執(zhí)行此命令可把操作馬上同步到磁盤上。
    • 例:
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

4. 用戶授權(quán)相關(guān):

  1. 查看用戶授權(quán):
  • 格式:
    • SHOW GRANTS [FOR 'user'@'host']
MariaDB [(none)]> show grants;          #不加用戶,默認(rèn)查找的是root用戶的授權(quán)信息。
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*128977E278358FF80A246B5046F51043A2B1FCED' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

MariaDB [(none)]> show grants for 'test0'@'192.168.1.%';        #查看指定用戶的授權(quán)信息。
+---------------------------------------------------------------------------------------------------------------+
| Grants for test0@192.168.1.%                                                                                  |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test0'@'192.168.1.%' IDENTIFIED BY PASSWORD '*5FC1DC57211AE5F87FC504DEEE4B7C65DEB2CBFA'|
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  1. 給用戶授權(quán):
  • 格式:
GRANT  priv_type [(column_list)] [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    TO user_specification [, user_specification] ...
    [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
    [WITH with_option ...]
  • 簡(jiǎn)化格式:
GRANT  priv_type ON  [object_type] priv_level TO user_specification [, user_specification]

    object_type:
        TABLE
        | FUNCTION
        | PROCEDURE

    priv_level:
        *
        | *.*                       #所有庫(kù)的所有表
        | db_name.*                 #某個(gè)庫(kù)的所有表
        | db_name.tbl_name          #某個(gè)庫(kù)的某個(gè)表
        | tbl_name                  #某個(gè)表
        | db_name.routine_name      #某個(gè)庫(kù)的某個(gè)routine
  • 示例:
MariaDB [(none)]> GRANT all ON *.* TO 'test0'@'192.168.1%';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR 'test0'@'192.168.1%';
+------------------------------------------------------------------------------------------------------------------------+
| Grants for test0@192.168.1.%                                                                                           |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'test0'@'192.168.1.%' IDENTIFIED BY PASSWORD '*5FC1DC57211AE5F87FC504DEEE4B7C65DEB2CBFA'|
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
# 
  1. 取消授權(quán):REVOKE
  • 格式
REVOKE  priv_type [(column_list)][, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    FROM  'user'@'host' [,  'user'@'host'] ...

REVOKE ALL PRIVILEGES, GRANT OPTION
    FROM user [, user] ...
  • 示例:
MariaDB [(none)]> REVOKE ALL PRIVILEGES FROM 'test0'@'192.168.1.%';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM 'test0'@'192.168.1%'' at line 1
MariaDB [(none)]> REVOKE ALL PRIVILEGES ON *.* FROM 'test0'@'192.168.1.%';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR 'test0'@'192.168.1%';
+---------------------------------------------------------------------------------------------------------------+
| Grants for test0@192.168.1.%                                                                                  |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test0'@'192.168.1.%' IDENTIFIED BY PASSWORD '*5FC1DC57211AE5F87FC504DEEE4B7C65DEB2CBFA'|
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI