MariaDB—— 7. mysql基本語句總結

1. 啓停數據庫

/usr/bin/mysqld_safe &
mysqladmin -uroot shutdown
mysqladmin -uroot shutdown -p

2. 連接數據庫

mysql -u root -p
mysql -u root -p123123
mysql -u root -D test -p123123
mysql -u root -p -S /var/lib/mysql/mysql.sock
mysql -u root -p -h 192.168.1.103 -P 3306
mysql -u root -p123123 -e 'use mysql; select user,host,password from user;'
mysql -uroot -p123123 -e 'create database if not exists testdb; show databases;'

連接數據庫的常用選項

--host=host_name, -h host_name:服務端地址
--user=user_name, -u user_name:用戶名
--port=port_num, -P port_num:服務端端口
--socket=path, -S path
--database=db_name, -D db_name
--compress, -C:數據壓縮傳輸
--execute=statement, -e statement:非交互模式執行SQL語句
--vertical, -E:查詢結果縱向顯示
--protocol={TCP|SOCKET|PIPE|MEMORY}

2. 變量

mysql中變量可以分爲系統變量與狀態變量。系統變量配置了mysql的運行環境屬性。狀態變量顯示了mysql運行過程中的狀態信息。而系統變量從作用域劃分,又可以分爲全局變量和會話變量。全局級別的變量:golbal variables,作用域爲整個mysql服務器。會話級別的變量:session variables,作用域爲當前會話。可以通過修改這些系統變量達到設置mysql屬性的目的。狀態變量也分爲全局級別與會話級別,但是用戶無法設置狀態變量,只能查看。

2.1 查看全局級別的系統變量

show global variables \G;
show global variables like ‘%version%’ \G;
show global variables where variable_name like ‘character%’;
show global variables where variable_name like ‘%log%’ and value=‘off’;

2.2 查看會話級別的系統變量

會話級別的所有參數往往比全局的更多,因爲它不僅繼承了全局的部分參數,可能還有一些額外的參數值。
使用如下語句查看會話級別的參數值。
show session variables \G;
如果不指明global或者session,則表示查看會話級別的變量值,如下語句表示顯示會話級別的變量。
show variables \G;
查看單個全局系統變量或者會話系統變量。
SELECT @@[global.|session.]system_var_name
select @@global.pid_file;
select @@session.warning_count;

MariaDB [(none)]> select @@global.pid_file;
+---------------------------+
| @@global.pid_file         |
+---------------------------+
| /var/lib/mysql/master.pid |
+---------------------------+
1 row in set (0.00 sec)

如果查看的系統遍歷不是會話級別的,系統會給提示:

MariaDB [(none)]> select @@session.pid_file;
ERROR 1238 (HY000): Variable 'pid_file' is a GLOBAL variable
2.3 獲取變量值

在寫存儲過程時,如果需要調用系統變量的值,可以通過如下方法調用。
@@GLOBAL.var_name
@@SESSION.var_name
同樣 @@var_name 表示優先從會話級別獲取變量值。

2.4 查詢mysql運行中的統計信息或狀態數據
MariaDB [(none)]> show status;
+--------------------------------------------------------------+--------------------------------------------------+
| Variable_name                                                | Value                                            |
+--------------------------------------------------------------+--------------------------------------------------+
| Aborted_clients                                              | 0                                                |
| Aborted_connects                                             | 1                                                |
| Access_denied_errors                                         | 0                                                |
| Acl_column_grants                                            | 0                                                |
| Acl_database_grants                                          | 0                                                |
| Acl_function_grants                                          | 0                                                |
| Acl_procedure_grants                                         | 0                                                |
2.5 修改變量值
2.5.1 運行時修改

運行時修改global級別的變量:對修改之前建立的會話沒有影響,僅對修改後新建立的會話有效;
運行時修改session級別的變量:僅對當前會話有效,且立即生效;

2.5.2 通過配置文件修改

需重啓後生效,但是永久有效。
通過set命令,運行時修改對應的變量。

SET GLOBAL var_name = value;
SET @@GLOBAL.var_name = value;  
SET SESSION var_name = value;  
SET @@SESSION.var_name = value;  

不指定級別表示默認爲設置會話級別的變量

SET var_name = value;  

2. 用戶

2.1 查詢用戶
MariaDB [(none)]> select host,user,password from mysql.user;
 
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *E3DCEF5BF0200CB2A09780D418826A2A3B1D2EC3 |
| master    | root | *E3DCEF5BF0200CB2A09780D418826A2A3B1D2EC3 |
| 127.0.0.1 | root | *E3DCEF5BF0200CB2A09780D418826A2A3B1D2EC3 |
| ::1       | root | *E3DCEF5BF0200CB2A09780D418826A2A3B1D2EC3 |
+-----------+------+-------------------------------------------+
MariaDB [(none)]> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
2.2 創建用戶

不推薦直接操作mysql.user來創建用戶

MariaDB [(none)]> create user gege@'%' identified by 'gege';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all on *.* to liuzexuan@'%' identified by '12345';
Query OK, 0 rows affected (0.00 sec)
2.3 刪除用戶

不推薦直接操作mysql.user來刪除用戶

MariaDB [(none)]> drop user liuzexuan@'%' ;
Query OK, 0 rows affected (0.00 sec)
2.3 重命名用戶
MariaDB [(none)]> rename user gege to gege1;
Query OK, 0 rows affected (0.01 sec)
2.4 修改用戶密碼
2.4.1 方法一
MariaDB [(none)]> set password for gege1@'%' = password("123456");
Query OK, 0 rows affected (0.00 sec)
2.4.2 方法二
[root@master ~]# mysqladmin -ugege1 -p123456 password 1234
2.4.3 方法三
mysqld_safe --skip-grant-tables &
mysql -uroot
mysql> UPDATE mysql.user SET password=PASSWORD("new password") WHERE user='root';
mysql> FLUSH PRIVILEGES;
2.5 用戶權限
2.5.1 授權
GRANT ALL [PRIVILEGES] ON db.tbl TO 'username'@'host' IDENTIFIED BY 'password';
grant select privileges on zsythink.* to zsy@'192.168.%.%';

grant insert,delete,update,select on zsythink.* to zsy@'192.168.%.%';
grant select (name,age) on zsythink.students to zsy@localhost;
grant execute on function zsythink.test to zsy@'192.168.%.%';
grant execute on procedure zsythink.test to zsy@'192.168.%.%';
#賦予和取消ssl
grant usage on *.* to 'zsy'@'222.222.222.222' require ssl;
grant usage on *.* to 'zsy'@'222.222.222.222' require none;

除了上面提到的grant option,管理員還可以通過如下選項對用戶進行一些其他的限制
MAX_QUERIES_PER_HOUR:限制用戶每小時執行的查詢語句數量;
MAX_UPDATES_PER_HOUR:限制用戶每小時執行的更新語句數量;
MAX_CONNECTIONS_PER_HOUR:限制用戶每小時連接數據庫的次數;
MAX_USER_CONNECTIONS:限制用戶使用當前賬號同時連接服務器的連接數量;
上述各限制選項的示例如下。

grant select on *.* to zsy@'192.168.%.%' identified by '123456' with max_queries_per_hour 20;
grant select on *.* to zsy@'192.168.%.%' identified by '123456' with max_updates_per_hour 10;
grant select on *.* to zsy@'192.168.%.%' identified by '123456' with max_connections_per_hour 15;
grant select on *.* to zsy@'192.168.%.%' identified by '123456' with max_user_connections 2;
2.5.2 查看權限

show grants for zsy@localhost;
如果想要查看用戶對哪些庫擁有哪些權限
select * from mysql.db where Db=“你要查看的數據庫”

2.5.3 移除權限

revoke “要移除的權限” on 數據庫.表 from 用戶@host;
revoke all on word.* from [email protected];

3. 數據庫

create database if not exists testdb default character set utf16;
show create database testdb;
show character set;

查看排序方式

show collation;

查看當前數據庫信息

MariaDB [mysql]> status
--------------
mysql  Ver 15.1 Distrib 10.2.31-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:		12
Current database:	mysql
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server:			MariaDB
Server version:		10.2.31-MariaDB MariaDB Server
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/var/lib/mysql/mysql.sock
Uptime:			16 min 54 sec

alter database testdb character set utf8;
alter database testdb default character set utf8;
drop database if exists testdb;

4. 數據表

show table;
show table status\G;
show table status like “*” \G;
show create table table_name;

create table s like d;

alter table test1 rename as test2;
alter table ttt add column age int;
alter table ttt add age int;
alter table ttt add age int not null default 0;
alter table ttt add column age int not null default 0;
alter table ttt add id int first;
alter table ttt add column age int after name;

alter table tt drop stuname;
既可以修改字段名稱又可以修改字段類型
alter table testtable change name name1 char(5);
不用修改字段名稱了
alter table testtable modify age int;

5. 約束

非空約束
alter table testtb modify name varchar(100) not null;
自動增長
alter table testtb modify id int auto_increment;
主鍵約束
alter table testtb4 add primary key(id);
alter table testtb4 add constraint primary key(id);
alter table testtb drop primary key;
唯一鍵約束
alter table testtb add unique key(uid);
指定唯一鍵的名稱uni_test
alter table testtb add unique key uni_test(test);
alter table testtb drop index uni_test;
查看約束
select * from information_schema.key_column_usage where table_name=‘test1’;
查看主外鍵對應關係
select TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
from information_schema.KEY_COLUMN_USAGE
where TABLE_NAME = ‘test1’ and REFERENCED_TABLE_NAME is not null;
添加外鍵約束
alter table testtb add column tid int default 0 not null;
alter table testtb add constraint testtb_tid_fk foreign key(tid) references testtb2(id)
刪除外鍵約束
select TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
from information_schema.KEY_COLUMN_USAGE
where TABLE_NAME = ‘test4’ and REFERENCED_TABLE_NAME is not null;
alter table test4 drop foreign key test_tid_fk

6. 索引

添加索引
alter table testtb add index ind_name(name);
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [using index_type] ON tbl_name (index_col_name,…)
create index ind_name on testtb (name(20) desc);
create index ind_name on testtb (name(20));
create index ind_name on testtb (name);
創建聯合索引
create index ind_id_name on testtb1 (id,name);
create index ind_id_name on testtb1 (id,name(20));
重建索引
repair table table_name quick;
刪除索引
alter table test2 drop index uu_ttt;
查看索引
show index from testtb;
show index from testtb where key_name like ‘ind%’;

————Blueicex 2020/3/28 11:32 [email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章