1. 程式人生 > 實用技巧 >五、MySQL使用者許可權管理、連線管理、啟動關閉流程、配置管理、內部快捷命令

五、MySQL使用者許可權管理、連線管理、啟動關閉流程、配置管理、內部快捷命令

一、使用者許可權管理

1.授權的命令

grant all on *.* to root@'172.16.1.%' identified by '123';

grant all on *.* to root@'172.16.1.%' identified by '123' with grant option;

2.作用物件

grant all on *.* to root@'172.16.1.%' identified by '123';     #所有庫下的所有表
grant all on mysql.* to root@'172.16.1.%' identified by '123'; #mysql庫下的所有表,單庫授權
grant all on mysql.user to root@'172.16.1.%' identified by '123'; #單表授權

#單列授權,企業稱之為脫敏,脫敏:脫離敏感資訊
1.普通使用者只能檢視使用者名稱字
mysql> grant select(user) on mysql.user to diaosi@'172.16.1.%' identified by '123';
2.VIP使用者可以檢視使用者使用者較多的資訊
mysql> grant select(user,host) on mysql.user to diaosi@'172.16.1.%' identified by '123';
3.超級VIP可以檢視使用者所有的資訊
mysql> grant select on mysql.user to diaosi@'172.16.1.%' identified by '123';

3.在企業裡一般我們怎麼給許可權

#開發要一個使用者登入資料庫
1.跟開發溝通:
1)你要操作的是哪個庫哪個表
2)你要對這個表進行哪些操作
3)你從哪個IP連過來
4)你對使用者名稱的要求
5)你對密碼有什麼要求
6)你的使用者要用多久
7)發郵件
2.一般怎麼授權
grant select,insert,update on mysql.* to dev@'172.16.1.51' identified by 'Dev@123456';
3.開發要root使用者怎麼辦?

4.許可權實踐

1)準備資料庫

#建立wordpress資料庫
create database wordpress;
#使用wordpress庫
use wordpress;
#建立t1、t2表
create table t1 (id int);
create table t2 (id int);

#建立blog庫
create database blog;
#使用blog庫
use blog;
#建立t1表
create table tb1 (id int);

2)授權

#授權wordpress@'10.0.0.5%'這個使用者對所有庫所有表有查詢許可權,密碼是123
#授權wordpress可以通過10.0.0.5%這個網段連線資料庫對所有庫所有表有查詢許可權,密碼是123
1.grant select on *.* to wordpress@'10.0.0.5%' identified by '123';

#授權wordpress@'10.0.0.5%'對wordpress庫下的所有表有增刪改的許可權,密碼是123
#授權wordpress可以通過10.0.0.5%這個網段連線資料庫對wordpress庫下的所有表有增刪改的許可權,密碼是123
2.grant insert,delete,update on wordpress.* to wordpress@'10.0.0.5%' identified by '123';

#授權wordpress@'10.0.0.5%'對wordpress下面的t1表有所有許可權,密碼是123
3.grant all on wordpress.t1 to wordpress@'10.0.0.5%' identified by '123';

3)請問

一個客戶端程式使用wordpress使用者登陸到10.0.0.51的MySQL後,
1.對t1表的管理能力? #所有許可權
2.對t2表的管理能力? #增刪改查
3.對tb1表的管理能力? #只有查

4)授權總結

1.不同級別授權,許可權是相加關係
2.不推薦在多級別定義重複的許可權
3.常見的授權方式是單庫授權
grant select,insert,update on mysql.* to dev@'172.16.1.51' identified by 'Dev@123456';
4.如果涉及到使用者的敏感資訊,可以使用脫敏,單列授權
5.檢視使用者的許可權
mysql> show grants for root@'172.16.1.%';

二、MySQL連線管理

1.連線工具

1)MySQL自帶的客戶端命令

#自帶的命令 mysql
-u: 指定使用者 mysql -uroot
-p: 指定密碼 mysql -uroot -p123
-h: 指定主機 mysql -uroot -p123 -h172.16.1.51
-P: 指定埠 mysql -uroot -p123 -P3307 -h172.16.1.51
-S: 指定sock mysql -uroot -p123 -S /tmp/mysql.sock
-e: 指定SQL mysql -uroot -p123 -e "show databases"
#--protocol=name:指定連線方式

2)第三方客戶端工具

1.Navicat
2.SQLyog

2.連線方式

1)socket連線

mysql -uroot -p123
mysql -uroot -p123 -S /tmp/mysql.sock

2)TCP/IP連線

mysql -uroot -p123 -h172.16.1.51

三、MySQL啟動關閉流程

1.啟動資料庫

1./etc/init.d/mysqld start --->  mysqld_safe --->  mysqld 

2.systemctl start mysqld ---> mysqld_safe ---> mysqld

3.mysqld_safe --defaults-file=/etc/my.cnf & ---> mysqld_safe ---> mysqld

2.關閉資料庫

#推薦的方式:
1./etc/init.d/mysqld stop
2.systemctl stop mysqld
3.mysqladmin shutdown
4.service mysql stop

#不推薦的方式:
1.kill -9 mysqlpid
2.pkill mysqld
3.killall mysqld
#錯誤關閉會導致:
1.資料丟失
2.資料庫啟動故障

四、MySQL配置管理

1.配置mysql的方法

1.編譯的時候
#程式存放位置
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.38 \
#資料存放位置
-DMYSQL_DATADIR=/application/mysql-5.6.38/data \
#socket檔案存放位置
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.38/tmp/mysql.sock \
... ...

2.初始化
/mysql_install_db --defaults-file=/service/3309/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3309/data

--datadir=/application/mysql/data
--basedir=/application/mysql
--defaults-file=/etc/my.cnf
--pid-file=/application/mysql/data/db01.pid
--socket=/application/mysql/data/mysql.sock
--user=mysql
--port=3306
--log-error=/application/mysql/data/db01.err

3.配置檔案
/etc/my.cnf
/etc/mysql/my.cnf
$basedir/my.cnf
~/my.cnf

#defaults-extra-file (類似include)

2.資料庫配置檔案

1)配置檔案都有哪些

/etc/my.cnf
/etc/mysql/my.cnf
$basedir/my.cnf
~/.my.cnf

2)配置檔案的讀取順序

/etc/my.cnf
/etc/mysql/my.cnf
$basedir/my.cnf
~/.my.cnf

3)配置檔案生效順序

~/.my.cnf
$basedir/my.cnf
/etc/mysql/my.cnf
/etc/my.cnf

4)測試配置檔案生效順序

#1.配置/etc/my.cnf
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
server_id=1

#2.配置/etc/mysql/my.cnf
[root@db02 ~]# vim /etc/mysql/my.cnf
[mysqld]
server_id=2

#3.配置$basedir/my.cnf
[root@db02 ~]# vim /service/mysql/my.cnf
[mysqld]
server_id=3

#4.配置~/my.cnf
[root@db02 ~]# vim ~/.my.cnf
[mysqld]
server_id=4

#5.重啟資料庫
[root@db02 ~]# systemctl stop mysqld
[root@db02 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

#6.檢視server_id
[root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 4 |
+---------------+-------+

#7.刪除生效的配置重啟,再次檢視id
[root@db02 ~]# rm -rf ~/.my.cnf
[root@db02 ~]# /etc/init.d/mysqld restart
[root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3 |
+---------------+-------+

#8.刪除生效的配置重啟,再次檢視id
[root@db02 ~]# rm -rf /service/mysql/my.cnf
[root@db02 ~]# /etc/init.d/mysqld restart
[root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+

#9.刪除生效的配置重啟,再次檢視id
[root@db02 ~]# rm -rf /etc/mysql/my.cnf
[root@db02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+

3.執行引數優先順序

1)socket檔案指定位置

#cmake:
socket=/application/mysql/tmp/mysql.sock

#命令列:
--socket=/tmp/mysql.sock

#配置檔案:
/etc/my.cnf 中[mysqld]標籤下:socket=/opt/mysql.sock

#default引數:
--defaults-file=/tmp/a.txt 配置檔案中[mysqld]標籤下:socket=/tmp/test.sock

2)測試優先順序

#1.啟動MySQL,檢視sock檔案位置
[root@db02 ~]# mysqld_safe --defaults-file=/tmp/a.txt --socket=/tmp/mysql.sock &
[root@db02 ~]# ll /tmp/
srwxrwxrwx 1 mysql mysql 0 Oct 22 18:58 mysql.sock

#2.啟動MySQL,檢視sock檔案位置
[root@db02 ~]# mysqld_safe --defaults-file=/tmp/a.txt &
[root@db02 ~]# ll /tmp/
srwxrwxrwx 1 mysql mysql 0 Oct 22 19:01 test.sock
#如果設定--defaults-file,那麼MySQL啟動時只讀取指定的配置檔案

#3.啟動MySQL,檢視sock檔案位置
[root@db02 ~]# mysqld_safe &
[root@db02 ~]# ll /opt/
srwxrwxrwx 1 mysql mysql 0 Oct 22 19:03 mysql.sock

4.優先順序總結

#優先順序排序
命令列 > --defaults-file引數指定配置 > ~/.my.cnf > $basedir/my.cnf > /etc/mysql/my.cnf > /etc/my.cnf > 初始化配置 > cmake

5.配置檔案作用

1)作用

1.影響客戶端的連線
2.影響服務端的啟動

2)影響客戶端的連線

[root@db02 ~]# vim /etc/my.cnf
#配置檔案新增連線資料庫資訊的配置
[mysql]
user=root
password=123456

3)影響服務端的啟動

#修改server_id
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
server_id=2

#檢視配置沒有生效
[root@db02 ~]# mysql -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+

#重啟資料庫後,修改的服務端配置才會生效
[root@db02 ~]# systemctl restart mysqld
[root@db02 ~]# mysql -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+

4)總結

1.客戶端配置受配置檔案影響
2.客戶端配置修改後不需要重啟服務 [mysql] 或者 [client]
3.服務端配置修改後需要重啟服務後生效 [mysqld] 或者 [server]

4.常見配置
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
server_id=2
socket=/tmp/mysql.sock

[mysql]
socket=/tmp/mysql.sock

五、MySQL的命令

1.mysql內部快捷命令

\c:     終止正在輸入的命令
\r: 重新連線資料庫
\d: 修改sql語句結束符
\e: 輸出輸入的內容
\G: 以key:values形式展示資料
\q: 退出資料庫
\g: 結束語句
\h: 檢視幫助
\T: 輸出一個檔案記錄操作
\t: 終止記錄操作的檔案
\p: 列印輸入的命令
\R: 臨時修改命令提示符
\. 匯入sql語句,等於 source 命令
\s: 檢視資料庫連線狀態
\!: 在資料庫裡執行命令列命令
\u 切換資料庫,等於 use 命令

2.help命令

#檢視資料庫內命令語法
mysql> help
mysql> help create database
mysql> help create table
mysql> help select

2.客戶端mysqladmin命令

1)設定密碼

#1.設定密碼
[root@db02 ~]# mysqladmin -uroot password
New password:
Confirm new password:

#2.修改密碼
[root@db02 ~]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:

2)關閉服務

[root@db02 ~]# mysqladmin -uroot -p shutdown 
Enter password: 123

3)庫外建庫

[root@db02 ~]# mysqladmin -uroot -p123 create database222222

4)庫外刪庫

[root@db02 ~]# mysqladmin -uroot -p123 drop database222222
Warning: Using a password on the command line interface can be insecure.
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'database222222' database [y/N] y
Database "database222222" dropped

5)檢視資料庫配置

[root@db02 ~]# mysqladmin -uroot -p123 variables | grep -w server_id

6)檢測資料庫是否啟動

[root@db02 ~]# mysqladmin -uroot -p ping
Enter password:
mysqld is alive

7)檢視資料庫資訊

[root@db02 ~]# mysqladmin -uroot -p123 status
Warning: Using a password on the command line interface can be insecure.
Uptime: 130 Threads: 1 Questions: 94 Slow queries: 0 Opens: 70 Flush tables: 1 Open tables: 63 Queries per second avg: 0.723

Slow queries: 0
Queries per second avg: 0.723

8)重新整理授權表

[root@db02 ~]# mysqladmin -uroot -p123 reload

9)重新整理binlog

[root@db02 ~]# mysql -uroot -p123 -e "show master status"
Warning: Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+

#重新整理binlog
[root@db02 ~]# mysqladmin -uroot -p123 flush-logs
Warning: Using a password on the command line interface can be insecure.

#再次檢視binlog
[root@db02 ~]# mysql -uroot -p123 -e "show master status"
Warning: Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+