下文主要給大家?guī)砉芾鞰ySQL用戶主要有哪些功能,希望這些文字能夠帶給大家實際用處,這也是我編輯管理MySQL用戶主要有哪些功能這篇文章的主要目的。好了,廢話不多說,大家直接看下文吧。
成都創(chuàng)新互聯(lián)公司是專業(yè)的平泉網(wǎng)站建設(shè)公司,平泉接單;提供網(wǎng)站制作、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行平泉網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
1. MySQL用戶管理
1 2 3 4 5 | 'user'@'host'; host:IP、主機名、NETWORK、%(任意長字符),_(任意單個字符) skip_name_resolve={ON|OFF} 跳過主機名解析 [root@node2 ~]# vim /etc/my.cnf skip_name_resolve=ON |
2.查看用戶
示例:
1 | MariaDB [mysql]> SELECT User,Host,Password FROM user; |
3.創(chuàng)建用戶
CREATE USER 'user'@'host' [IDENTIFIED BY [PASSWORD] 'password'] [,'user'@'host' [IDENTIFIED BY [PASSWORD] 'password']...]
示例:
1 | MariaDB [(none)]> CREATE USER 'tom'@'127.0.0.1' IDENTIFIED BY 'liumanlin' , 'jerry'@'%' IDENTIFIED BY 'liumanlin'; |
4.重命名:RENAME USER
RENAME USER old_user TO new_user[, old_user TO new_user] ...
示例:
1 | MariaDB [mysql]> RENAME USER 'tom'@'127.0.0.1' TO 'jerry'@'172.18.%.%'; |
5.刪除用戶
DROP USER 'user'@'host' [, 'user'@'host'] ...
示例:
1 2 | MariaDB [mysql]> DROP USER 'jerry'@'%'; MariaDB [mysql]> DROP USER ''@'localhost'; |
6.讓MySQL重新加載授權(quán)列表
FLUSH PRIVILEGES;
示例:
1 | MariaDB [mysql]> FLUSH PRIVILEGES; |
7.修改用戶密碼
(1) SET PASSWORD [FOR 'user'@'host'] = PASSWORD('cleartext password'); PASSWORD是MySQL內(nèi)建加密函數(shù)
示例:
1 2 | MariaDB [mysql]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('liumanlin'); MariaDB [mysql]> FLUSH PRIVILEGES; |
(2) UPDATE mysql.user SET Password=PASSWORD('cleartext password') WHERE User='USERNAME' AND Host='HOST';
示例:
1 2 | MariaDB [mysql]> UPDATE user SET Password=PASSWORD('liumanlin') WHERE User='root' AND Host='127.0.0.1'; MariaDB [mysql]> FLUSH PRIVILEGES; |
(3) mysqladmin -uUSERNAME -hHOST -p password 'NEW_PASS'
示例:
1 | [root@node2 ~]# mysqladmin -h227.0.0.1 -uroot -p password 'liumanlin'; |
8.忘記管理員密碼的解決辦法
(1) 啟動mysqld進程時,使用--skip-grant-tables和--skip-networking選項
示例:
CentOS 7:
1 2 3 4 | [root@node2 ~]# vim /usr/lib/systemd/system/mariadb.service ExecStart=/usr/bin/mysqld_safe --basedir=/usr --skip-grant-tables --skip-networking [root@node2 ~]# systemctl daemon-reload [root@node2 ~]# systemctl restart mariadb.service |
CentOS 6:
1 | [root@node2 ~]# vim /etc/init.d/mysqld 同理 |
(2) 通過UPDATE命令修改管理員密碼
示例:
1 2 3 | MariaDB [mysql]> UPDATE user SET Password=PASSWORD('liumanlin') WHERE User='root' AND Host='127.0.0.1'; [root@node2 ~]# vim /usr/lib/systemd/system/mariadb.service ExecStart=/usr/bin/mysqld_safe --basedir=/usr |
(3) 以正常方式啟動mysqld進程;
示例:
1 2 | [root@node2 ~]# systemctl daemon-reload [root@node2 ~]# systemctl restart mariadb.service |
9.授權(quán):GRANT
1 2 3 4 5 | 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 ...] |
object_type(對象類型):
TABLE
| FUNCTION
| PROCEDURE
priv_level:
*
| *.*(所有庫的所有表)
| db_name.*(指定庫的所有表)
| db_name.tbl_name(指定庫的指定表)
| tbl_name(指定表)
| db_name.routine_name(指定庫的指定函數(shù))
ssl_option:
SSL
| X509
| CIPHER 'cipher'
| ISSUER 'issuer'
| SUBJECT 'subject'
with_option:
GRANT OPTION
| MAX_QUERIES_PER_HOUR count
| MAX_UPDATES_PER_HOUR count
| MAX_CONNECTIONS_PER_HOUR count
| MAX_USER_CONNECTIONS count
示例1:
1 2 3 4 5 6 7 8 9 10 | MariaDB [mysql]> GRANT CREATE ON hidb.* TO 'jerry'@'172.18.%.%'; [root@node2 ~]# mysql -ujerry -h272.18.67.12 -p MariaDB [(none)]> CREATE DATABASE hidb; MariaDB [(none)]> use hidb; MariaDB [hidb]> CREATE TABLE tbl1 (name CHAR(20)); MariaDB [hidb]> CREATE INDEX test ON tbl1(name); ERROR 1142 (42000): INDEX command denied to user 'jerry'@'172.18.67.12' for table 'tbl1' (無權(quán)創(chuàng)建索引,用以下方法) MariaDB [mysql]> GRANT INDEX ON hidb.* TO 'jerry'@'172.18.%.%'; MariaDB [mysql]> SHOW GRANTS FOR 'jerry'@'172.18.%.%'; MariaDB [hidb]> CREATE INDEX test ON tbl1(name); (授權(quán)成功) |
示例2:
1 2 3 4 5 6 7 8 | MariaDB [mysql]> CREATE USER 'tom'@'172.18.%.%' IDENTIFIED BY 'liumanlin'; [root@node2 ~]# mysql -utom -h272.18.67.12 -p (可正常登錄) MariaDB [mysql]> GRANT ALL ON hidb.* TO 'tom'@'172.18.%.%' REQUIRE SSL; (使用ssl授權(quán)登錄) MariaDB [mysql]> SHOW GRANTS FOR 'tom'@'172.18.%.%'; MariaDB [mysql]> FLUSH PRIVILEGES; [root@node2 ~]# mysql -utom -h272.18.67.12 -p Enter password: ERROR 1045 (28000): Access denied for user 'tom'@'172.18.67.12' (using password: YES) (無法連接,需指明ssl證書) |
示例3:
1 2 3 4 5 6 7 8 9 10 11 12 | MariaDB [mysql]> SHOW GLOBAL VARIABLES LIKE '%ssl%'; +---------------+----------+ | Variable_name | Value | +---------------+----------+ | have_openssl | DISABLED | | have_ssl | DISABLED | | ssl_ca | | | ssl_capath | | | ssl_cert | | | ssl_cipher | | | ssl_key | | +---------------+----------+ |
10.查看授權(quán):SHOW GRANTS
SHOW GRANTS [FOR 'user'@'host']
示例:
1 | MariaDB [mysql]> SHOW GRANTS FOR 'tom'@'172.18.%.%'; |
11.取消授權(quán):REVOKE
1 2 3 4 5 | 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] ... |
示例:
1 2 3 | MariaDB [mysql]> REVOKE CREATE VIEW ON hidb.* FROM 'tom'@'172.18.%.%'; MariaDB [mysql]> SHOW GRANTS FOR 'tom'@'172.18.%.%'; MariaDB [mysql]> FLUSH PRIVILEGES; |
對于以上關(guān)于管理MySQL用戶主要有哪些功能,大家是不是覺得非常有幫助。如果需要了解更多內(nèi)容,請繼續(xù)關(guān)注我們的行業(yè)資訊,相信你會喜歡上這些內(nèi)容的。