一、mysql運行原理
?? ?mysql的運行分為三層
?? ?客戶端和連接服務
?? ?
?? ?核心服務功能(sql接口、緩存的查詢、sql的分析和優化以及部分內置函數的執行等。)
?? ?存儲引擎層(負責mysql中數據的存儲和提取。)
?
二、示例
1、實驗環境單臺mysql1主+1從
2、mysql單實例故障排查
現象1:ERROR 2002 (HY000):can't connect to local MySQL server through socket '/data/mysql/mysql.sock'(2)分析:一般是數據庫未啟動、mysql配置文件未指定socket文件或者數據庫端口被防火墻攔截導致。解法:啟動數據庫或者防火墻開放數據庫監聽端口。現象2:ERROR 1045 (28000):Access denied for user 'root'@'localhost' (using password:NO)分析:密碼不正確或沒有權限訪問。解法:修改my.cnf主配置文件,在[mysqld]下添加skip-grant-talbes=on,重啟數據庫,最后修改密碼命令如下。 5.7版本
mysql> update mysql.user set authentication_string=password('123456') where user='root' and Host='localhost';
mysql> flush privileges;8.0版本
mysql> update mysql.user set authentication_string='' where user='root' and Host='localhost';
mysql> flush privileges;
mysql> alter user 'root'@'localhost' identified by '123456';再刪除剛添加的skip-grant-tables參數,重啟數據庫,使用新密碼即可登錄。重新授權,命令如下。5.7版本
mysql> grant all on *.* to 'root'@'mysql-server' identified by '123456';8.0版本
mysql> create user 'root'@'mysql-server' identified by '123456';
mysql> grant all on *.* to 'root'@'mysql-server';現象3:在使用遠程連接數據庫時偶爾會發生遠程連接數據庫很慢的問題。分析:開啟了dns解析功能。解法:修改my.cnf主配置文件,在[mysqld]下添加skip-name-resolve,重啟數據庫可以解決。注意在以后授權里面不能再使用主機名授權。現象4:Can't open file: 'xxx_forums.MYI'. (error:145)分析:服務器非正常關機,數據庫所在空間已滿,或一些其它未知的原因,對數據庫表造成了孫環。可能是操作系統下直接將數據庫文件拷貝移動,會因為文件的屬組問題而產生這個錯誤。解法:myisamchk -r 數據文件目錄/數據表名.MYI; # 僅適合獨立主機用戶。或修改文件的屬組。復制數據庫文件的過程中沒有將數據庫文件設置為MySQL運行的賬號可讀寫。 # 僅適合獨立主機用戶。現象5:ERROR 1129 (HY000):Host 'xxx.xxx.xxx.xxx' is blocked because of many connection errors;unblock with 'mysqladmin flush-hosts'分析:超出了mysql的總連接請求數,新的連接會無法連接。解法:使用mysqladmin flush-hosts命令清除緩存即可。mysqladmin -uroot -p -h ip flush-hosts 或修改mysql配置文件,在[mysqld]下面添加max_connect_errors=1000,然后重啟。現象6:客戶端報 Too many connections.分析:連接數超出mysql的最大連接數限制。解法:在my.cnf配置文件中增大連接數,然后重啟mysqlmax_connections=1000或臨時修改最大連接數。set GLOBAL max_connections=1000;現象7:Warning:World-writable config file '/etc/my.cnf' is ignoredERROR! MySQL is running but PID file could not be found分析:mysql的配置文件/etc/my.cnf權限不對。解法:chmod 644 /etc/my.cnf現象8:InnoDB: Error: page 14178 log sequence number 29455369832InnoDB: is in the future! Current system log sequence number 29455369832分析:innodb數據文件損壞。解法:修改my.cnf配置文件,在[mysqld]下添加innodb_force_recovery=4,啟動數據庫后備份數據文件,然后去掉該參數,利用備份文件恢復數據。MySQL主從故障排查現象1從庫的Slave_IO_Running 為 NOThe slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).分析:主庫和從庫的server-id值一樣。解法:修改從庫的server-id值,與主庫不一樣,重啟mysql后再同步即可。現象2從庫的Slave_IO_Running 為 NO分析:原因很多,例如主鍵沖突、主庫刪除,從庫找不到記錄、數據被修改導致。通常狀態碼報錯有1007、1032、1062、1452等。解法:
mysql> stop slave;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; # 設置全局sql從屬計數器為1.
mysql> start slave;或mysql> set global read_only=true; # 設置用戶權限,從庫只讀。現象3Error initializing relay log position: I/O error reading the header from the binary log分析:從庫的中繼日志relay-log損壞。解法:手工修復,重新找到同步的binlog和pos點,然后重新同步即可。mysql>chan gemaster to master_log_file='mysql-bin.xxxxxx',master_log_pos=xxx;mysql的sql語句優化創建測試表并插入數據
mysql> create database test;
Query OK, 1 row affected (0.02 sec)mysql> use test;
Database changed
mysql> create table users(-> id int primary key auto_increment,-> name varchar(50) not null,-> email varchar(100) not null,-> age int not null,-> created_at datetime default current_timestamp-> );
Query OK, 0 rows affected (0.04 sec)mysql> delimiter $$ # 設置分隔符,用$$來作為當前語句的終止符,以便后續存儲過程使用它作為分隔符。
mysql> create procedure ii_users() # 創建存儲程序-> begin # 開始存儲程序。-> declare i int default 0; # 聲明變量。-> while i < 100000 do # 執行循環插入數據。當i小于10萬時,進入循環,name列由concat('user',i)生成,如user1、user2等,email列由concat('user',i,'@example.com')生成,如'user1@example.com',age列通過floor(rand() * 100) 隨機生成0到99之間的整數值。 通過 i = i + 1 來更新變量的值。-> insert into users(name,email,age) -> values (concat('user',i),concat('user',i,'@example.com'),-> floor(rand() * 100));-> set i = i + 1;-> end while; # 結束循環-> end$$ #結束存儲程序,使用之前的$$作為終止符。
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ; # 恢復語句分隔符,將默認的語句分隔符恢復為; 避免后續查詢出錯。
mysql> call ii_users(); # 執行存儲過程。
Query OK, 1 row affected (6 min 34.95 sec)mysql> explain select * from users where name = 'user123';
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
| 1 | SIMPLE | users | NULL | ALL | NULL | NULL | NULL | NULL | 99909 | 10.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.00 sec)mysql> alter table users add index idx_name(name); # 優化,創建索引。
Query OK, 0 rows affected (0.68 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> explain select * from users where name = 'user123'; # 查詢使用了索引,大大提高了查詢效率。
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | users | NULL | ref | idx_name | idx_name | 152 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+explian用于顯示mysql如何執行一條sql語句,關鍵字段解釋:
字段 說明 優化關注點
id 查詢序列號,相同id為同一執行層,不同id按序執行。select_type 查詢類型(simple、primary、subquery、derived等) 識別子查詢或臨時表操作。table 訪問的表名或別名。 確認查詢涉及的表。type 訪問類型 性能從優到劣:system > const > eq_ref > ref > range > index > ALL 避免ALL(全表掃描),優先優化為ref或range。possible_keys 可能使用的索引 檢查是否有合適索引未被使用。key 實際使用的索引 確認是否命中最佳索引rows 預估掃描的行數 行數越少,查詢效率越高。extra 附加信息(如 using where 、using index、using temporary等) 發現潛在性能問題(如臨時表、文件排序)。