MySQL的一点学习笔记

  • 2019 年 11 月 6 日
  • 筆記

一点MySQL的学习笔记

MySQL存储引擎的一点学习笔记

最近在看《MySQL技术内幕之innodb存储引擎》这本上,上面还是写了很多有意义有价值的内容的,这里讲学习笔记贴上来,学习笔记中包含很多点,以后再系统的总结一把,今天先把所有的点罗列一些,大家可以看看,希望有所帮助吧。

1.配置文件

在启动MySQL数据库的过程中,可以不指定默认的配置文件,MySQL会按照编译时的默认参数设置启动实例,用下面的明星可以查看当MySQL数据库实例启动时,会在哪些位置查找自己的配置文件:

[root@dev01 ~]# mysql --help|grep my.cnf                        order of preference, my.cnf, $MYSQL_TCP_PORT,  /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf  [root@dev01 ~]#  

可以看到,MySQL数据库是按照上述顺序取访问my.cnf数据库的,这里读者可能会抛出来一个问题,如果这几个配置文件中都包含同一个参数,那么MySQL数据库以哪一个配置文件为准?这个问题的答案是MySQL会以最后一个配置文件中的参数为准,一般情况情况下,Linux中的配置文件在/etc/my.cnf

2.datadir

在配置文件中有一个参数datadir,这个参数指定了数据库所在的路径,在Linux中默认的路径是/usr/local/mysql/data,这个参数也可以通过下面的SQL语句去查询:

mysql> show variables like 'datadir';  +---------------+--------------+  | Variable_name | Value        |  +---------------+--------------+  | datadir       | /data/mysql/ |  +---------------+--------------+   row in set (. sec)  

3.存储引擎

查看MySQL当前版本所包含的存引擎以及当前的默认存储引擎:

mysql> mysql> show engines;  +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+  | Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |  +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+  | CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |  | MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |  | MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |  | BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |  | PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |  | MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |  | ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |  | InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |  | FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |  +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+   rows in set (. sec)  

从上面的结果中我们可以看到,innodb是当前MySQL服务的默认存储引擎。

4.两种连接MySQL的方法

连接MySQL的操作是一个连接进程和MySQL数据库实例进行通信,我们知道常用的通信方式有以下几种,分别是管道、命名管道、命名字、TCP/IP套接字、UNIX套接字等MySQL常用的连接方式有两种,一种是TCP/IP方式,一种是UNIX方式,分别将这两种连接方式做简要说明:

TCP/IP

TCP/IP套接字方式时MySQL数据库在任何平台下都提供的连接方式,也是网络中使用的最多的一种方式,这种方式在TCP/IP连接上简历一个基于网络的连接请求,一般情况下client和server不在同一台机器上,二者通过网络进行连接,举例如下:

[root@dev01 ~]# mysql -uroot -h127.0.0. -p  Enter password:  Welcome to the MySQL monitor.  Commands end with ; or g.  Your MySQL connection id is  Server version: 5.7. MySQL Community Server (GPL)    Copyright (c) , , Oracle and/or its affiliates. All rights reserved.    Oracle is a registered trademark of Oracle Corporation and/or its  affiliates. Other names may be trademarks of their respective  owners.    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.    mysql>  

这里我们使用了127.0.0.1地址,也就是本地的地址,实际上,它可以换作任意一个授权的服务地址,我们可以使用下面的方法来查询某个地址是否被授权:

mysql 22:25:59>>select user,host from mysql.user;  +------------------+-----------------+  | user             | host            |  +------------------+-----------------+  | cacti            | 10.10.29.%      |  | cacti            | 10.30..%      |  | dba_admin        | 127.0.0.1       |  | dba_dbmonitor    | 127.0..       |  | srv_dbmonitor_ro | 127.0.0.1       |  

| monitor | 192.168.xxx.xxx |

| dev_tkcils_rwh   | 192.168.17.%    |  | dev_gongdan_rwl  | 192.168..%    |  | root             | localhost       |  | shutdown_user    | localhost       |  | tkadmin          | localhost       |  +------------------+-----------------+   rows in set (. sec)  

UNIX套接字方式

在Linux和UNIX环境下,还可以使用UNIX套接字方式,UNIX套接字实际上不是一个网络协议,它只能应用在服务器和客户端在同一网络的情况使用,用户可以在配置文件中指定套接字的路径,如–socket=/tmp/mysql.sock等,我们可以通过下面的命令来查找当前服务的套接字路径:

mysql> show variables like 'socket';  +---------------+-----------------+  | Variable_name | Value           |  +---------------+-----------------+  | socket        | /tmp/mysql.sock |  +---------------+-----------------+   row in set (. sec)  

下面我们看看如何使用UNIX套接字进行登录:

[root@dev01 ~]# ps -ef| grep mysqld  root              : ?        :: /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/dev01.pid  mysql          : ?        :: /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysqld.log --pid-file=/data/mysql/dev01.pid --socket=/tmp/mysql.sock  root         : pts/    :: grep mysqld  [root@dev01 ~]# mysql --socket=/tmp/mysql.sock -pjjmatch  mysql: [Warning] Using a password on the command line interface can be insecure.  Welcome to the MySQL monitor.  Commands end with ; or g.  Your MySQL connection id is  Server version: 5.7.19 MySQL Community Server (GPL)    Copyright (c) , , Oracle and/or its affiliates. All rights reserved.    Oracle is a registered trademark of Oracle Corporation and/or its  affiliates. Other names may be trademarks of their respective  owners.    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.    mysql>  

5.MySQL中的文件种类

参数文件:

my.cnf:它告诉MySQL实例启东市在哪里可以找到数据库文件,并且制定某些初始化参数,这些参数定义了某种内存结构的大小等设置,还会介绍各种参数的类型

日志文件:

mysql-bin.000001:用来记录MySQL实例对某种条件作出相应时写入的文件,如错误日志文件,二进制日志文件,慢查询日志文件,查询日志文件等

socket文件

mysql.sock,当用UNIX套接字方式进行连接时候需要的文件

pid文件

mysql.pid,tashi MySQL实例的进程ID文件

MySQL表结构文件

用来存放MySQL表结构定义的文件

存储引擎文件

MySQL的存储引擎都会有自己的文件来保存各种数据,这些存储引擎真正存储了记录和索引等数据