MariaDB [db2]> use information_schema;
Database changed
MariaDB [information_schema]> SELECT * FROM triggers WHERE trigger_name='trigger_student_count_insert'\G
1. 增長學員觸發器
MariaDB [db2]> CREATE TRIGGER <==建立命令
-> trigger_student_count_insert <==觸發器名稱
-> AFTER INSERT <==觸發時間
-> ON student_info FOR EACH ROW <==做用在表student_info的每一行
-> UPDATE student_count SET student_count=student_count+1;<==觸發條件
MariaDB [db2]> CREATE TRIGGER trigger_student_count_delete
-> AFTER DELETE
-> ON student_info FOR EACH ROW
-> UPDATE student_count SET student_count=student_count-1;
MariaDB [hellodb]> grant select(stuid,name) on hellodb.students to v9@'192.168.50.100''在192.168.50.100主機上使用帳號v9查看授予的權限'
MariaDB [hellodb]> desc students;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment || Name | varchar(50)| NO || NULL ||
+-------+------------------+------+-----+---------+----------------+
受權而且建立帳號
1
MariaDB [(none)]> grant all on *.* to v9@'192.168.50.109' IDENTIFIED BY '123456';
回收權限指令 REVOKE
語法格式
1
2
3
4
REVOKE
priv_type[(column_list)][ , priv_type[(column_list)]]...
ON [object_type] priv_level
FROM user [, user]...
MariaDB [(none)]> REVOKE SELECT (stuid,name)
-> ON `hellodb`.`students`
-> FROM v9@'192.168.50.100';
查看指定用戶得到的受權:
使用幫助 Help SHOW GRANTS
查詢指定用戶權限
語法格式 SHOW GRANTS FOR ‘user’@‘host’;
示例
1
2
3
4
5
6
7
MariaDB [hellodb]>SHOW GRANTS FOR v9@'192.168.50.100';
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'v9'@'192.168.50.100' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
鏈接權限
| GRANT SELECT (name, stuid) ON `hellodb`.`students` TO 'v9'@'192.168.50.100'
查詢權限 |
+----------------------------------------------------------------------------------------------------------
'查看mysql數據庫中文件的結構'
MariaDB [(none)]> show table status from mysql\G
*************************** 30. row ***************************
Name: user
Engine: MyISAM <==存儲引擎爲 MyISAM
Version: 10
Row_format: Dynamic
......省略
Comment: Users and global privileges
PERFORMANCE_SCHEMA數據庫 MySQL 5.5開始新增的數據庫,主要用於收集數據庫服務器性能參數(例如鏈接狀況、負載狀況),庫裏表的存儲引擎均爲PERFORMANCE_SCHEMA,用戶不能建立存儲引擎爲PERFORMANCE_SCHEMA的表
1
2
3
4
MariaDB [(none)]> show table status from performance_schema\G
*************************** 52. row ***************************
Name: users
Engine: PERFORMANCE_SCHEMA
information_schema數據庫 MySQL 5.0以後產生的,一個虛擬數據庫,物理上並不存在。information_schema數據庫相似與「數據字典」,提供了訪問數據庫元數據的方式,即數據的數據。好比數據庫名或表名,列類型,訪問權限(更加細化的訪問方式),使用的存儲引擎爲MEMORY
1
2
3
4
MariaDB [(none)]> show table status from information_schema\G
*************************** 75. row ***************************
Name: INNODB_SYS_SEMAPHORE_WAITS
Engine: MEMORY
example 「stub」引擎,它什麼都不作。可使用此引擎建立表,但不能將數據存儲在其中或從中檢索。目的是做爲例子來講明如何開始編寫新的存儲引擎
4.6 管理存儲引擎
查看mysql支持的存儲引擎: show engines;
查看當前默認的存儲引擎:
1
MariaDB [(none)]> SHOW VARIABLES LIKE '%storage_engine%'
設置默認的存儲引擎:
1
2
3
vim /etc/my.conf
[mysqld]
default_storage_engine=InnoDB <==在[mysqld]中增長系統此選項
查看庫中全部表使用的存儲引擎 Show table status from db_name;
1
2
'查看hellodb數據庫中表的引擎'
MariaDB [(none)]> SHOW table STATUS FROM hellodb\G
查看庫中指定表的存儲引擎
經過查看錶狀態信息獲取 show table status like ’ tb_name’;
1
2
3
4
5
6
7
'進入表所在數據庫'
MariaDB [(none)]> use hellodb
'查詢表students所用引擎'
MariaDB [hellodb]> SHOW table STATUS LIKE 'students'\G
*************************** 1. row ***************************
Name: students
'Engine: InnoDB'<==存儲引擎
'建立默認引擎爲Innodb的表cc'
MariaDB [db2]> CREATE TABLE cc(StuID int unsigned NOT NULL,Name varchar(50) NOT NULL) ENGINE=Innodb;
修改表存儲引擎 ALTER TABLE tb_name ENGINE=InnoDB;
1
2
3
4
5
6
7
'修改表cc的存儲引擎爲MyISAM'
MariaDB [db2]> ALTER TABLE cc ENGINE=MyISAM;'查看修改效果'
MariaDB [db2]> SHOW TABLE STATUS LIKE 'cc'\G
*************************** 1. row ***************************
Name: cc
Engine: MyISAM
aria_block_size <==配置選項|變量
Description: Block size to be used for Aria index pages. Changing this requires dumping, deleting old tables and deleting all log files, and then restoring your Aria tables. If key lookups take too long (and one has to search roughly 8192/2 by default to find each key), can be made smaller, e.g. 2048 or 4096.
Commandline: --aria-block-size=# <==是否支持命令行
Scope: Global <==變量範圍級別
Dynamic: No <==是否支持動態修改
Data Type: numeric <==數據類型
Default Value: 8192 <==默認值
系統變量
系統變量查詢
格式 支持模糊查詢,默認爲SESSION級別的 SHOW VARIABLES [LIKE 'Variable_name'] 查GLOBAL級別的變量 SHOW GLOBAL VARIABLES [LIKE 'Variable_name]'
示例
1
2
3
4
5
6
7
8
9
'列出全部會話系統變量'
MariaDB [(none)]> SHOW VARIABLES;'查找指定會話系統變量'
MariaDB [(none)]>SHOW VARIABLESlike LIKE '%datadir%';
+---------------+--------------+
| Variable_name | Value |
+---------------+--------------+
| datadir | /data/mysql/ |<==變量的鍵值
+---------------+--------------+
常見的系統變量 max_connections:容許最大併發鏈接數
1
2
MariaDB [hellodb]> show variables like '%connect%';| max_connections | 151 |
修改服務器變量的值:
獲取設置SET幫助 mysql> help SET
修改全局變量:僅對修改後新建立的會話有效;對已經創建的會話無效 SET GLOBAL system_var_name=value SET @@global.system_var_name=value
1
2
MariaDB [db2]> SET GLOBAL max_connections=500;
MariaDB [db2]> SET @@GLOBAL.max_connections=600;
修改會話變量: SET [SESSION] system_var_name=value SET @@[session.] system_var_name=value
狀態變量(只讀) 用於保存mysqld運行中的統計數據的變量,不可更改
格式 查看全局狀態變量 SHOW GLOBAL STATUS [LIKE ‘Variable_name’] 查看會話狀態變量 SHOW [SESSION] STATUS [LIKE ‘Variable_name’]
示例 uptime:查看服務器啓動時間
1
MariaDB [(none)]> show status like 'uptime';
Com_select:查看使用查詢的次數
1
2
MariaDB [hellodb]> show status like '%select%';| Com_select | 4 |<==查詢計數器
Com_insert:查看插入計數器
1
2
MariaDB [hellodb]> show status like '%insert%';| Com_insert | 1 |
Threads_connected:查看多少人正在鏈接數據庫,Threads爲進程項
1
2
3
MariaDB [hellodb]> show status like '%Threads%';| Threads_connected | 1 |<==正在鏈接的
| Threads_running | 1 |<==正在運行的