MySQL杂记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL杂记相关的知识,希望对你有一定的参考价值。

mysql

基础

1、查看mysql支持的引擎

MariaDB [(none)]> SHOW ENGINES;

2、设置mysql环境变量

[[email protected] ~]# vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH

查看mysql运行时默认选项

[[email protected] ~]# mysql --print-defaults
mysql would have been started with the following arguments:

[[email protected] ~]# 

3、打印mysql详细设置

[[email protected] ~]# mysql --print-defaults --verbose

4、不进入数据库直接执行SQL语句

[[email protected] ~]# mysql -e ‘SHOW DATABASES;‘
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[[email protected] ~]# 

5、显示配置文件

[[email protected] ~]# mysql --verbose --help

6、显示mysql统计数据

MariaDB [(none)]> SHOW GLOBAL STATUS;

7、模糊匹配通配符

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘sql_mode‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_mode      |       |
+---------------+-------+
1 row in set (0.01 sec)

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘sql_%‘;

1、创建测试数据库

MariaDB [(none)]> CREATE DATABASE mydb;
Query OK, 1 row affected (0.02 sec)

MariaDB [(none)]> use mydb;
Database changed
MariaDB [mydb]> 

创建表:
MariaDB [mydb]> CREATE TABLE t1 (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,name CHAR(5) NOT NULL);

2、违反规则插入字段

MariaDB [mydb]> INSERT INTO t1 (name) VALUES (‘tom‘),(‘BlackBerry‘);
Query OK, 2 rows affected, 1 warning (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 1

查看警告:
MariaDB [mydb]> SHOW WARNINGS;
+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1265 | Data truncated for column ‘name‘ at row 2 |
+---------+------+-------------------------------------------+
1 row in set (0.00 sec)

警告显示:自动做了修剪;

MariaDB [mydb]> SELECT * FROM t1;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  2 | Black |
+----+-------+
2 rows in set (0.00 sec)

3、修改当前会话模型后测试

MariaDB [mydb]> SET sql_mode=‘TRADITIONAL‘;

测试报错,不让插入数据:
MariaDB [mydb]> INSERT INTO t1 (name) VALUES (‘tom‘),(‘RedBerry‘);
ERROR 1406 (22001): Data too long for column ‘name‘ at row 2
MariaDB [mydb]> SELECT * FROM t1;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  2 | Black |

4、测试别的模式

MariaDB [mydb]> SET GLOBAL sql_mode=‘STRICT_ALL_TABLES‘;
Query OK, 0 rows affected (0.01 sec)

改完了也没用,对当前会话无效
MariaDB [mydb]> 
MariaDB [mydb]> 
MariaDB [mydb]> SHOW VARIABLES LIKE ‘sql_mode‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_mode      |       |
+---------------+-------+
1 row in set (0.01 sec)

对接下来创建的会话有效:
MariaDB [mydb]> SHOW GLOBAL VARIABLES LIKE ‘sql_mode‘;
+---------------+-------------------+
| Variable_name | Value             |
+---------------+-------------------+
| sql_mode      | STRICT_ALL_TABLES |
+---------------+-------------------+
1 row in set (0.00 sec)

新建立会话有效:
MariaDB [(none)]> SHOW VARIABLES LIKE ‘sql_mode‘;
+---------------+-------------------+
| Variable_name | Value             |
+---------------+-------------------+
| sql_mode      | STRICT_ALL_TABLES |
+---------------+-------------------+
1 row in set (0.00 sec)

基础

1、查看CREATE帮助

MariaDB [(none)]> HELP CREATE

查看INSERT帮助
MariaDB [(none)]> HELP INSERT

2、查看数据库默认引擎

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%default%engine%‘;
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+

3、查看创建这张表的状态

MariaDB [mydb]> SHOW TABLE STATUS LIKE ‘t1‘;

MariaDB [mydb]> SHOW TABLE STATUS LIKE ‘t1‘\G
*************************** 1. row ***************************
           Name: t1
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 2              #行数
 Avg_row_length: 8192           #行评价长度
    Data_length: 16384          #表大小
Max_data_length: 0  #表数据最大容量,去存储引擎有关,0表示没上限
   Index_length: 0  #索引大小
      Data_free: 10485760       #在内存未保存表空间大小
 Auto_increment: 5              #自动增长字段
    Create_time: 2017-01-19 03:34:39
    Update_time: NULL       #最近一次表更新时间
     Check_time: NULL       #最近一次表检查时间
      Collation: latin1_swedish_ci  #排序规则
       Checksum: NULL   #校验和
 Create_options:        #创建表的额外选项
        Comment:        #注释
1 row in set (0.01 sec)

4、

以上是关于MySQL杂记的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主从复制杂记

mysql安装杂记

mysql优化杂记

mysql5.6 使用杂记

mysql 操作杂记

MySQL主从复制杂记