MySQL 不完全手册 随时更新中.....
基本操作
-
登录 Mariadb 数据库
1
2mysql -h host -P 3306 -u root -p
//其中 host 为服务器地址; 3306 为默认端口号 root 为登录用户名 -p为密码 -
在mysql cli 模式下调用 Linux 命令
1
2# system + linux cmd
system pwd -
退出登录 Mariadb
1
quit
-
查看 Mariadb 版本
1
select version();
-
应用指定数据库
1
use database;
-
查看当前选择使用的数据库
1
select database();
-
查看所有数据库
1
show databases;
-
查看默认认证方式
Mysql Mariadb 数据中,用户信息存放在默认的 mysql 数据库的 user 表中
1
select host, user, plugin from user;
-
查看数据库默认编码
1
status;
或者:
1
show variables like 'char%';
-
显示安装后可以使用的存储引擎和默认引擎
1
show engines;
MySQL 帮助系统
为什么使用内置帮助系统
在mysql的使用过程中, 可能经常会遇到以下问题:
1. 某个操作语法忘记了, 如何快速查找?
2. 如何快速知道当前版本上某个字段类型的取值范围?
3. 当前版本都支持哪些函数?希望有例子说明..
4. 当前版本是否支持某个功能?
对于上面列出的问题, 我们可能想到的方法是查找MySQL的文档, 这些问题在官方文档都可以很清楚地查到, 但是却要耗费大量的时间和精力.
所以对于以上问题, 最好的解决办法就是使用MySQL安装后自带的帮助文档, 这样在遇到问题时就可以方便快捷地进行查询.
帮助系统的使用
- 最简单的 help
1 | help |
help contents;
或者:
? contents;
#显示如下:
You asked for help about help category: “Contents”
For more information, type ‘help
categories:
Account Management
Administration
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Procedures
Sequences
Table Maintenance
Transactions
User-Defined Functions
Utility
1 |
|
help data types;
或者:
? data types;
#显示如下:
You asked for help about help category: “Data Types”
For more information, type ‘help
topics:
AUTO_INCREMENT
BIGINT
BINARY
BIT
BLOB
BLOB and TEXT Data Types
BOOLEAN
CHAR
CHAR BYTE
DATE
DATETIME
DECIMAL
DOUBLE
ENUM
FLOAT
Geometry Types
JSON Data Type
LONGBLOB
LONGTEXT
MEDIUMBLOB
MEDIUMINT
MEDIUMTEXT
Numeric Data Type Overview
ROW
SET Data Type
SMALLINT
String Literals
TEXT
TIME
TIMESTAMP
TINYBLOB
TINYINT
TINYTEXT
VARBINARY
VARCHAR
YEAR Data Type
1 |
|
help DATE
或者:
? DATE
#显示如下:
Name: ‘DATE’
Description:
Syntax
DATE
Description
A date. The supported range is ‘1000-01-01’ to
‘9999-12-31’. MariaDB
displays DATE values in ‘YYYY-MM-DD’ format, but can be
assigned dates in looser formats, including strings or
numbers, as long as they make sense. These include a short
year, YY-MM-DD, no delimiters, YYMMDD, or any other
acceptable delimiter, for example YYYY/MM/DD. For details,
see date and time literals.
‘0000-00-00’ is a permitted special value (zero-date),
unless the NO_ZERO_DATE SQL_MODE is used. Also, individual
components of a date can be set to 0 (for example:
‘2015-00-12’), unless the NO_ZERO_IN_DATE SQL_MODE is
used. In many cases, the result of en expression involving a
zero-date, or a date with zero-parts, is NULL. If the
ALLOW_INVALID_DATES SQL_MODE is enabled, if the day part is
in the range between 1 and 31, the date does not produce any
error, even for months that have less than 31 days.
Examples
CREATE TABLE t1 (d DATE);
INSERT INTO t1 VALUES (“2010-01-12”), (“2011-2-28”),
(‘120314’),(‘130421’);
SELECT * FROM t1;
±-----------+
| d |
±-----------+
| 2010-01-12 |
| 2011-02-28 |
| 2012-03-14 |
| 2013-04-21 |
±-----------+
1 |
|
help administration
#显示如下:
You asked for help about help category: "Administration"
For more information, type 'help <item>', where <item> is one of the following
topics:
About SHOW
BINLOG
CACHE INDEX
FLUSH
FLUSH QUERY CACHE
FLUSH TABLES FOR EXPORT
HELP Command
KILL [CONNECTION | QUERY]
LOAD INDEX
RESET
SET
SHOW AUTHORS
SHOW BINARY LOGS
SHOW BINLOG EVENTS
SHOW CHARACTER SET
SHOW CLIENT_STATISTICS
SHOW COLLATION
SHOW COLUMNS
SHOW CONTRIBUTORS
SHOW CREATE DATABASE
SHOW CREATE EVENT
SHOW CREATE FUNCTION
SHOW CREATE PACKAGE
SHOW CREATE PACKAGE BODY
SHOW CREATE PROCEDURE
SHOW CREATE SEQUENCE
SHOW CREATE TABLE
SHOW CREATE TRIGGER
SHOW CREATE USER
SHOW CREATE VIEW
SHOW DATABASES
SHOW ENGINE
SHOW ENGINE INNODB STATUS
SHOW ENGINES
SHOW ERRORS
SHOW EVENTS
SHOW EXPLAIN
SHOW FUNCTION CODE
SHOW FUNCTION STATUS
SHOW GRANTS
SHOW INDEX
SHOW LOCALES
SHOW MASTER STATUS
SHOW OPEN TABLES
SHOW PACKAGE BODY STATUS
SHOW PACKAGE STATUS
SHOW PLUGINS
SHOW PLUGINS SONAME
SHOW PRIVILEGES
SHOW PROCEDURE CODE
SHOW PROCEDURE STATUS
SHOW PROCESSLIST
SHOW PROFILE
SHOW PROFILES
SHOW QUERY_RESPONSE_TIME
SHOW RELAYLOG EVENTS
SHOW SLAVE HOSTS
SHOW SLAVE STATUS
SHOW STATUS
SHOW TABLE STATUS
SHOW TABLES
SHOW TABLE_STATISTICS
SHOW TRIGGERS
SHOW USER_STATISTICS
SHOW VARIABLES
SHOW WARNINGS
SHOW WSREP_MEMBERSHIP
SHOW WSREP_STATUS
1
2
3
4
5
6
# 字符集编码
1. 查看支持的字符集
show charset;
# or
show character set;
1
2
3
4
5
{% asset_img show_charset.png show_charset %}
2. 查看数据库默认字符集及排序规则
show variables like "%character%";show variables like "%collation%";
1
2
3
4
5
{% asset_img show_variables_like_charset.png show_variables_like_charset %}
3. 设置编码格式为 utf8mb4
SET NAMES 'utf8mb4';
set character_set_server = utf8mb4;
set character_set_database = utf8mb4;
set collation_database = utf8mb4_general_ci;
set collation_server = utf8mb4_general_ci;
1
2
3
4
5
6
7
8
9
10
{% asset_img set_names_utf8mb4.png set_names_utf8mb4 %}
```但是,重启 mariadb-server 后 重新查询显示如下```
{% asset_img set_names_reload.png set_names_reload %}
可以发现,重新回复了默认值;由此可见:用语句设置编码方式是临时性质
如果要永久设置编码方式,那么 只能在数据库配置文件 也就是 ```/etc/my.cnf``` 设置编码格式:
[client-server]
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
log-error=/var/log/mysqld.log
init-connect='SET NAMES utf8mb4'
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
1
2
3
4
5
6
7
8
9
重启查询:
{% asset_img my_cnf_set_utf8mb4.png my_cnf_set_utf8mb4 %}
发现设置成功
<font color=#FF0000>如果你的数据库中已经存在表了,建议进行修复一下</font>
mysqlcheck -u root -p --auto-repair --optimize --all-databases
1
2
3
4
5
6
# Mysql权限与角色
1. 创建用户并授权
# 创建用户
create user 'user_name'@'%' identified with mysql_native_password by 'password';
#
# 授权
grant all privileges on database.tables to 'user_name'@'%' with grant option;
# 授权所有数据库的所有表所有权限
# grant all privileges on *.* to 'user_name'@'%' with grant option;
#
# 刷新使之生效
flush privileges;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
其中:
- user_name:用户名
- '%' 域
可选值:
%代表通配所有host地址权限(可远程访问)
localhost为本地权限(不可远程访问)
指定特殊Ip访问权限 如10.138.106.102
- password 密码
- database.tables 数据库.表 格式;如果是 *.* 表示所有数据库的所有表
2. 查看用户权限
show grants for '#userName'@'#host';
1
2
3
4
3. 撤销授权
REVOKE '#privilege' ON '#databasename'.'#tablename' FROM '#username'@'#host';
1
2
3
4
5
6
7
privilege 代表具体权限 比如: ALL select 等
注意用什么的语句授权就用什么样的语句收回权限,否则可能无法撤销权限
# 数据库操作
1. 创建数据库
create database { #database name } CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci Case Insensitive
1
2
3
4
5
6
- CHARACTER: 字符集(多数情况下要选择一个支持中文的字符集)
- COLLATE: 排序规则,一般来说每种CHARSET都有多种它所支持的COLLATE,并且每种CHARSET都指定一种COLLATE为默认值。例如Latin1编码的默认COLLATE 为 latin1_swedish_ci,GBK 编码的默认 COLLATE 为 gbk_chinese_ci,utf8mb4 编码的默认值为utf8mb4_general_ci Case Insensitive
2. 查看数据库创建库语句的SQL信息
show create database { #database_name };
1
2
3
4
5
{% asset_img mysql_show_create_database.png mysql_show_create_database %}
3. 查看数据表创建库语句的SQL信息
show create table { #table_name }
1
2
3
3. 修改已存在数据库编码
alter database { #database_name } character set utf8mb4;
1
2
3
4
5
{% asset_img alter_database_character.png alter_database_character %}
4. 显示当前数据库中所有表名称:
show tables;
# 或者
show tables from { #database_name }
1
2
3
4
{% asset_img show_tables.png show_tables %}
5. 显示表中列名
show columns from { #database_name.#table_name }
# 或者
desc { #database_name.#table_name }
```
{% asset_img desc_table.png desc_table %}