生活随笔
收集整理的這篇文章主要介紹了
MySQL性能调优 读写分离
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Top
NSD DBA2 DAY02 案例1:MySQL性能優(yōu)化 案例2:實(shí)現(xiàn)MySQL讀寫分離 1 案例1:MySQL性能優(yōu)化 1.1 問題 練習(xí)相關(guān)優(yōu)化選項 啟用慢查詢?nèi)罩?/li> 查看各種系統(tǒng)變量、狀態(tài)變量 1.2 步驟 實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行。
步驟一:mysql性能優(yōu)化
1)查看服務(wù)運(yùn)行時的參數(shù)配置
mysql> show variables\G; ...... *************************** 171. row *************************** Variable_name: innodb_log_file_size Value: 50331648 *************************** 172. row *************************** Variable_name: innodb_log_files_in_group Value: 2 *************************** 173. row *************************** Variable_name: innodb_log_group_home_dir Value: ./ *************************** 174. row *************************** Variable_name: innodb_log_write_ahead_size Value: 8192 *************************** 175. row *************************** Variable_name: innodb_lru_scan_depth Value: 1024 *************************** 176. row *************************** Variable_name: innodb_max_dirty_pages_pct Value: 75.000000 *************************** 177. row *************************** Variable_name: innodb_max_dirty_pages_pct_lwm Value: 0.000000 *************************** 178. row *************************** Variable_name: innodb_max_purge_lag Value: 0 ...... mysql> show variables like "%innodb%"; +------------------------------------------+------------------------+ | Variable_name | Value | +------------------------------------------+------------------------+ | ignore_builtin_innodb | OFF | | innodb_adaptive_flushing | ON | | innodb_adaptive_flushing_lwm | 10 | | innodb_adaptive_hash_index | ON | | innodb_adaptive_hash_index_parts | 8 | | innodb_adaptive_max_sleep_delay | 150000 | ...... ...... | innodb_undo_log_truncate | OFF | | innodb_undo_logs | 128 | | innodb_undo_tablespaces | 0 | | innodb_use_native_aio | ON | | innodb_version | 5.7.17 | | innodb_write_io_threads | 4 | +------------------------------------------+------------------------+ 134 rows in set (0.01 sec) 2)并發(fā)連接數(shù)量
查看當(dāng)前已經(jīng)使用的連接數(shù)
mysql> flush status; Query OK, 0 rows affected (0.00 sec) mysql> show global status like "Max_used_connections"; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | Max_used_connections | 3 | +----------------------+-------+ 1 row in set (0.00 sec) 查看默認(rèn)的最大連接數(shù)
mysql> show variables like "max_connections%"; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 151 | +-----------------+-------+ 1 row in set (0.00 sec) 3)連接超時時間
mysql> show variables like "%timeout%"; +-----------------------------+----------+ | Variable_name | Value | +-----------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | have_statement_timeout | YES | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 50 | | innodb_rollback_on_timeout | OFF | | interactive_timeout | 28800 | | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 60 | | wait_timeout | 28800 | +-----------------------------+----------+ 13 rows in set (0.00 sec) 4)允許保存在緩存中被重用的線程數(shù)量
mysql> show variables like "thread_cache_size"; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | thread_cache_size | 9 | +-------------------+-------+ 1 row in set (0.00 sec) 5)用于MyISAM引擎的關(guān)鍵索引緩存大小
mysql> show variables like "key_buffer_size"; +-----------------+---------+ | Variable_name | Value | +-----------------+---------+ | key_buffer_size | 8388608 | +-----------------+---------+ 1 row in set (0.00 sec) 6)為每個要排序的線程分配此大小的緩存空間
mysql> show variables like "sort_buffer_size"; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | sort_buffer_size | 262144 | +------------------+--------+ 1 row in set (0.00 sec) 7)為順序讀取表記錄保留的緩存大小
mysql> show variables like "read_buffer_size"; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | read_buffer_size | 131072 | +------------------+--------+ 1 row in set (0.01 sec) 8)為所有線程緩存的打開的表的數(shù)量
mysql> show variables like "table_open_cache"; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | table_open_cache | 2000 | +------------------+-------+ 1 row in set (0.00 sec) 步驟二:SQL查詢優(yōu)化
1)常用日志種類及選項,如圖-1所示:
圖-1
記錄慢查詢,圖-2所示:
啟用慢查詢?nèi)罩?/p>
[root@master10 ~]# vim /etc/my.cnf ... slow_query_log=1 slow_query_log_file=mysql-slow.log long_query_time=5 log_queries_not_using_indexes=1 ... [root@master10 ~]# systemctl restart mysqld 2)查看慢查詢?nèi)罩?/p>
[root@master10 ~]# mysqldumpslow /var/lib/mysql/mysql-slow.log Reading mysql slow query log from /var/lib/mysql/mysql-slow.log Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts 查看緩存的大小
mysql> show variables like "query_cache%"; +------------------------------+---------+ | Variable_name | Value | +------------------------------+---------+ | query_cache_limit | 1048576 | | query_cache_min_res_unit | 4096 | | query_cache_size | 1048576 | | query_cache_type | OFF | | query_cache_wlock_invalidate | OFF | +------------------------------+---------+ 5 rows in set (0.00 sec) 3)查看當(dāng)前的查詢緩存統(tǒng)計
mysql> show global status like "qcache%"; +-------------------------+---------+ | Variable_name | Value | +-------------------------+---------+ | Qcache_free_blocks | 1 | | Qcache_free_memory | 1031832 | | Qcache_hits | 0 | | Qcache_inserts | 0 | | Qcache_lowmem_prunes | 0 | | Qcache_not_cached | 40 | | Qcache_queries_in_cache | 0 | | Qcache_total_blocks | 1 | +-------------------------+---------+ 8 rows in set (0.00 sec) 2 案例2:實(shí)現(xiàn)MySQL讀寫分離 2.1 問題 搭建一主一從結(jié)構(gòu) 配置maxscale代理服務(wù)器 測試分離配置 2.2 方案 使用4臺RHEL 7虛擬機(jī),如圖-1所示。其中192.168.4.10和192.168.4.20,分別提供讀、寫服務(wù),均衡流量,通過主從復(fù)制保持?jǐn)?shù)據(jù)一致性,由MySQL代理192.168.4.100面向客戶端,收到SQL寫請求時,交給服務(wù)器A處理,收到SQL讀請求時,交給服務(wù)器B處理。linux客戶機(jī)用于測試配置,可以使用真機(jī)代替
圖-1
2.3 步驟 實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行。
步驟一:搭建主從
1)搭建一主一從結(jié)構(gòu),主庫192.168.4.10上面操作
[root@master10 ~]# vim /etc/my.cnf [mysqld] server_id=10???? log-bin=master10???????? ... [root@master10 ~]# systemctl restart mysqld???????? 2)從庫192.168.4.20上面操作
[mysqld] server_id=20???????????? log-bin=slave20???????? read_only=1???????????? ... [root@slave20 ~]# systemctl restart mysqld 3)主庫授權(quán)一個用戶并查看master的狀態(tài)
[root@master10 ~]# mysql -u root -p123456 mysql> grant all on *.* to 'replicater'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> show master status; +-----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-----------------+----------+--------------+------------------+-------------------+ | master10.000002 | 449 | | | | +-----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) 4)從庫通過CHANGE MASTER語句指定MASTER服務(wù)器的IP地址、同步用戶名/密碼、起始日志文件、偏移位置(參考MASTER上的狀態(tài)輸出)
[root@slave20 ~]# mysql -u root -p123456 mysql> change master to master_host='192.168.4.10', -> master_user='replicater', -> master_password='123456', -> master_log_file='master10.000002', -> master_log_pos=738; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.4.10 Master_User: replicater Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master10.000002 Read_Master_Log_Pos: 738 Relay_Log_File: slave20-relay-bin.000002 Relay_Log_Pos: 319 Relay_Master_Log_File: master10.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 738 Relay_Log_Space: 528 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 10 Master_UUID: 95ada2c2-bb24-11e8-abdb-525400131c0f Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) 5)測試,主庫創(chuàng)建aa庫
mysql> create database aa; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aa | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) 6)從庫上面查看,有aa庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aa | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) 步驟二:實(shí)現(xiàn)mysql讀寫分離
1)配置數(shù)據(jù)讀寫分離服務(wù)器192.168.4.100
環(huán)境準(zhǔn)備關(guān)閉防火墻和SElinux,保證yum源可以正常使用
[root@maxscale ~]# cd mysql/ [root@maxscale mysql]# ls maxscale-2.1.2-1.rhel.7.x86_64.rpm [root@maxscale mysql]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm ???????? warning: maxscale-2.1.2-1.rhel.7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 8167ee24: NOKEY Preparing... ################################# [100%] Updating / installing... 1:maxscale-2.1.2-1 ################################# [100%] 2)配置maxscale
[root@maxscale mysql]# vim /etc/maxscale.cnf.template [maxscale] threads=auto???????????? [server1]???????????? type=server address=192.168.4.10???????? port=3306 protocol=MySQLBackend???????? [server2] type=server address=192.168.4.20 port=3306 protocol=MySQLBackend [MySQL Monitor]???????????????? type=monitor module=mysqlmon servers=server1, server2???????? user=scalemon???????????????????? passwd=123456???????????????????? monitor_interval=10000???????? #[Read-Only Service]???????? #type=service #router=readconnroute #servers=server1 #user=myuser #passwd=mypwd #router_options=slave [Read-Write Service]???????????? type=service router=readwritesplit servers=server1, server2 user=maxscaled???????????? passwd=123456???????????????? max_slave_connections=100% [MaxAdmin Service]???????? type=service router=cli #[Read-Only Listener]???????? #type=listener #service=Read-Only Service #protocol=MySQLClient #port=4008 [Read-Write Listener]???????????? type=listener service=Read-Write Service protocol=MySQLClient port=4006 [MaxAdmin Listener]???????? type=listener service=MaxAdmin Service protocol=maxscaled socket=default port=4099???? 3)根據(jù)配置文件的設(shè)置,在數(shù)據(jù)庫服務(wù)器上添加授權(quán)用戶(主庫執(zhí)行,從庫查看)
mysql> grant replication slave,replication client on *.* to scalemon@'%' identified by "123456";???????????? Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant select on mysql.* to maxscaled@"%" identified by "123456"; Query OK, 0 rows affected, 1 warning (0.01 sec) 4)查看授權(quán)用戶
在主庫上面查看
mysql> select user,host from mysql.user where user in ("scalemon","maxscaled"); +-----------+------+ | user | host | +-----------+------+ | maxscaled | % | | scalemon | % | +-----------+------+ 2 rows in set (0.00 sec) 在從庫上面查看
mysql> select user,host from mysql.user where user in ("scalemon","maxscaled"); +-----------+------+ | user | host | +-----------+------+ | maxscaled | % | | scalemon | % | +-----------+------+ 2 rows in set (0.00 sec) 測試授權(quán)用戶
[root@maxscale mysql]# mysql -h 192.168.4.10 -u scalemon -p123456 [root@maxscale mysql]# mysql -h 192.168.4.20 -u scalemon -p123456 [root@maxscale mysql]# mysql -h 192.168.4.10 -u maxscaled -p123456 [root@maxscale mysql]# mysql -h 192.168.4.20 -u maxscaled -p123456 5)啟動服務(wù)
[root@maxscale ~]# maxscale -f /etc/maxscale.cnf [root@maxscale ~]# ps -C maxscale???????? PID TTY TIME CMD 17930 ? 00:00:00 maxscale [root@maxscale ~]# netstat -antup | grep maxscale tcp6 0 0 :::4099 :::* LISTEN 17930/maxscale tcp6 0 0 :::4006 :::* LISTEN 17930/maxscale 6)測試,在本機(jī)訪問管理端口查看監(jiān)控狀態(tài)
maxadmin -P端口 -u用戶名 -p密碼
[root@maxscale ~]# maxadmin -P4099 -uadmin -pmariadb MaxScale> MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.4.10 | 3306 | 0 | Master, Running server2 | 192.168.4.20 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- 7)在客戶端訪問讀寫分離服務(wù)器(沒有mysql命令可以安裝)
mysql -h讀寫分離服務(wù)ip -P4006 -u用戶名 -p密碼
[root@slave53 ~]# mysql -h192.168.4.100 -P4006 -ureplicater -p123456 mysql> select @@hostname;???????????? +------------+ | @@hostname | +------------+ | slave20 | +------------+ 1 row in set (0.00 sec) mysql> create table t2(id int(4) ); Query OK, 0 rows affected (0.02 sec) mysql> insert into aa.t2 values(777); Query OK, 1 row affected (0.01 sec) 在主庫上面查看
mysql> use aa mysql> select * from t2; +------+ | id | +------+ | 777 | +------+ 1 row in set (0.00 sec) 從庫(主庫同步到從庫)
mysql> use aa mysql> select * from t2; +------+ | id | +------+ | 777 | +------+ 1 row in set (0.00 sec)
轉(zhuǎn)載于:https://www.cnblogs.com/tiki/p/10783527.html
總結(jié)
以上是生活随笔 為你收集整理的MySQL性能调优 读写分离 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。