數(shù)據(jù)庫命令
(1)展示所有用戶
select user,host from mysql.user;
(2)提供專用連接線程
show processlist;
(3)全庫 show databases;
表
(4)表 use mysql show tables;
1.看圖詳解mysql
2.mysql體系結(jié)構(gòu)及基礎(chǔ)管理
2.1客戶端/服務(wù)器工作模型(c/s)
(1)本地scoket連接方式:
socket=/tmp/mysql.sock
mysql -S /tmp/mysql.sock
說明:只能在本地使用台夺,不依賴于ip和端口
(2) 遠(yuǎn)程tcpip連接方式
mysql -uroot -p123 -h 10.0.0.51 -p 3306
2.2服務(wù)端:實(shí)例
實(shí)例:mysql+工作哦線程+預(yù)分配的內(nèi)存結(jié)構(gòu)
功能:管理數(shù)據(jù)
3.mysqlserver層
3.1連接層
1.提供連接協(xié)議(socket账磺,tcp/ip)
2.驗(yàn)證
3.提供專用線程
mysq> show processlist;
3.2sql層
1.語法檢查
2.語義(DDL碎赢、DCL凡蜻、CML当窗、DTL..)
3.權(quán)限
4.解析器:解析預(yù)處理(沙盤)評估執(zhí)行此語句的方法有哪些A方案(全盤掃描 全表 ) B方案(索引) 得出執(zhí)行的計劃
5.優(yōu)化器:幫我們選擇他認(rèn)為最優(yōu)的方案(基于代價cost)
6.執(zhí)行器:按照優(yōu)化器的選擇執(zhí)行sql語句
得出執(zhí)行結(jié)果:你需要的數(shù)據(jù)在磁盤的什么位置
7.查詢緩存(query_cache 默認(rèn)不開啟)可以redis替代
8.日志記錄(binlog二進(jìn)制日志丐箩,golg晋控,需要認(rèn)為開啟)
3.3.存儲引擎層
相當(dāng)于linux文件系統(tǒng)峡谊,和磁盤交互的模塊
mysql處理語句流程圖
4.mysql的邏輯結(jié)構(gòu)(操作對象)
邏輯-->抽象
--------------------------------------------------------
linux
目錄:名字 +屬性
文件:文件名 + 文件屬性 +文件內(nèi)容
---------------------------------------------------------
mysql
庫 : 庫名 + 庫屬性
表 :表名 +表屬性 + 表內(nèi)容 + 列
---------------------------------------------------------
庫 show databases;
表 use mysql show tables;
列 desc user;
5.mysql 物理存儲結(jié)構(gòu)
段 :一個表就是一個段茫虽,可以由一個或者多個區(qū)夠成
區(qū) :一個區(qū)(簇),默認(rèn)1M既们,連續(xù)的64個pages
頁 :一個頁濒析,默認(rèn)16kb,連續(xù)的4個os block啥纸,最小的io單元
5.用戶管理
5.1.用戶的作用
linux用戶:
登錄linux系統(tǒng)
管理linux對象 文件 (linux一切都文件)
mysql用戶:
登錄musql數(shù)據(jù)庫
管理mysql對象 表 (mysql一切都表)
5.2用戶的定義
linux用戶:用戶名
mysql用戶:用戶名@‘白名單’
白名單
地址列表号杏,允許白名單的ip登錄mysql,管理mysql
oldliu@'localhost' :oldliu用戶能夠通過本地登錄mysql
oldliu@'10.0.0.10' :oldliu用戶
6.增刪改查
6.1查
select user,host ,authentication_string from mysql.user;
authentication_string 這是查看用戶有沒有密碼斯棒,不寫就會把所有用戶寫出來
MariaDB [(none)]> select user,host ,authentication_string from mysql.user;
+--------+-----------+-----------------------+
| user | host | authentication_string |
+--------+-----------+-----------------------+
| root | localhost | |
| root | 127.0.0.1 | |
| root | ::1 | |
| zabbix | localhost | |
+--------+-----------+-----------------------+
4 rows in set (0.00 sec)
6.2增
本地登錄MariaDB [(none)]> create user oldliu@'localhost';
ip登錄并設(shè)置密碼 create user oldyan@'10.0.0.%' identified by '123';
6.3改
6.4刪除
MariaDB [(none)]> drop user oldliu@'localhost';
Query OK, 0 rows affected (0.00 sec)
注意8.0版本以前盾致,是可以通過grant命令 建立用戶+權(quán)限
7.權(quán)限管理
(1)作用
用戶對數(shù)據(jù)庫對象,有哪些管理能力
(2)權(quán)限的表現(xiàn)方式
具體命令
MariaDB [(none)]> show privileges;
+-------------------------+---------------------------------------+-------------------------------------------------------+
| Privilege | Context | Comment |
+-------------------------+---------------------------------------+-------------------------------------------------------+
| Alter | Tables | To alter the table |
| Alter routine | Functions,Procedures | To alter or drop stored functions/procedures |
| Create | Databases,Tables,Indexes | To create new databases and tables |
| Create routine | Databases | To use CREATE FUNCTION/PROCEDURE |
| Create temporary tables | Databases | To use CREATE TEMPORARY TABLE |
| Create view | Tables | To create new views |
| Create user | Server Admin | To create new users |
| Delete | Tables | To delete existing rows |
| Drop | Databases,Tables | To drop databases, tables, and views |
| Event | Server Admin | To create, alter, drop and execute events |
| Execute | Functions,Procedures | To execute stored routines |
| File | File access on server | To read and write files on the server |
| Grant option | Databases,Tables,Functions,Procedures | To give to other users those privileges you possess |
| Index | Tables | To create or drop indexes |
| Insert | Tables | To insert data into tables |
| Lock tables | Databases | To use LOCK TABLES (together with SELECT privilege) |
| Process | Server Admin | To view the plain text of currently executing queries |
| Proxy | Server Admin | To make proxy user possible |
| References | Databases,Tables | To have references on tables |
| Reload | Server Admin | To reload or refresh tables, logs and privileges |
| Replication client | Server Admin | To ask where the slave or master servers are |
| Replication slave | Server Admin | To read binary log events from the master |
| Select | Tables | To retrieve rows from table |
| Show databases | Server Admin | To see all databases with SHOW DATABASES |
| Show view | Tables | To see views with SHOW CREATE VIEW |
| Shutdown | Server Admin | To shut down the server |
| Super | Server Admin | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc. |
| Trigger | Tables | To use triggers |
| Create tablespace | Server Admin | To create/alter/drop tablespaces |
| Update | Tables | To update existing rows |
| Usage | Server Admin | No privileges - allow connect only |
+-------------------------+---------------------------------------+-------------------------------------------------------+
31 rows in set (0.00 sec)
(3)授權(quán)回收權(quán)限操作
語法
8.0以前
garnt 權(quán)限 on to 用戶 identified by '密碼'顿锰;
8.0后開始
create 用戶 user identified by '密碼'煮落;
gant 權(quán)限(權(quán)限可以是多個) on 對象 to 用戶 identified by '密碼'悬槽;
權(quán)限介紹
ALL : 管理員
具體權(quán)限: 業(yè)務(wù)用戶 開發(fā) 運(yùn)維等
Grant option :
gant 權(quán)限(權(quán)限可以是多個) on 對象 to 用戶 identified by '密碼' with grant option;
對象: 庫 表
管理員 *.* :------> chmod -R 755 / 根下的所有
普通用戶 oldliu.* :-------> chmod 755 /oldliu /oldliu目錄下的所有
oldliu.t1 :--------> chmod 755 /oldliu/t1 /oldliu/t1下的某個目錄或者文件
(4)授權(quán)案例
案例1創(chuàng)建并授權(quán)管理員用戶护赊,能夠通過10.0.0.%網(wǎng)段登錄并管理數(shù)據(jù)庫
grant all on *.* oldliu@'10.0.0.%' identified by '123' with grant option;
查詢創(chuàng)建的用戶
select user,host from mysql.user;
查尋創(chuàng)建用戶的權(quán)限
show grants for oldliu@'10.0.0.%' ;
案例2創(chuàng)建并授權(quán)一個app@’10.0.0.%‘業(yè)務(wù)用戶,能夠?qū)pp庫下所有對象進(jìn)行create,update,select
grant create,update,select to app@'10.0.0.%' identified by '123';
查詢
show grants for app@'10.0.0.%';
(5)擴(kuò)展mysql 授權(quán)表 (都在mysql庫下)
user : *.*
db : app.*
tables_priv : app.t1
columns_priv : 列
(6)回收權(quán)限
linux中
chmod -R 755 /oldliu ------->chmod -R 644 /oldliu
###注意:mysql中不能通過重復(fù)授權(quán)修改權(quán)限砾跃,只能通過回收權(quán)限進(jìn)行修改
回收權(quán)限
revoke create on app.* from 'app'@'10.0.0.%‘百揭;
(7)查詢用戶的權(quán)限
select * from mysql.user/G
(8)超級管理員忘記密碼
--skip-grant-tables: 跳過授權(quán)表
--skip-networking:跳過TCP/IP連接\
#a.關(guān)閉數(shù)據(jù)庫
#b.使用安全模式啟動
mysql_safe --skip-grant-tables --skip-networking
或者
service mysql start --skip-grant-tables --skip-networking
#c.登錄數(shù)據(jù)庫并修改密碼
mysql 進(jìn)入數(shù)據(jù)庫
修改密碼會報錯,手工加載授權(quán)表
flush privileges;
#d.重啟數(shù)據(jù)庫
進(jìn)入數(shù)據(jù)庫用修改過后的密碼進(jìn)入