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]

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