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的存儲引擎都會有自己的文件來保存各種數據,這些存儲引擎真正存儲了記錄和索引等數據