MySQL性能调优 读写分离

Top

NSD DBA2 DAY02

  1. 案例1:MySQL性能优化
  2. 案例2:实现MySQL读写分离

1 案例1:MySQL性能优化

1.1 问题

  • 练习相关优化选项
  • 启用慢查询日志
  • 查看各种系统变量、状态变量

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:mysql性能优化

1)查看服务运行时的参数配置

  1. mysql> show variables\G;
  2. ......
  3. *************************** 171. row ***************************
  4. Variable_name: innodb_log_file_size
  5. Value: 50331648
  6. *************************** 172. row ***************************
  7. Variable_name: innodb_log_files_in_group
  8. Value: 2
  9. *************************** 173. row ***************************
  10. Variable_name: innodb_log_group_home_dir
  11. Value: ./
  12. *************************** 174. row ***************************
  13. Variable_name: innodb_log_write_ahead_size
  14. Value: 8192
  15. *************************** 175. row ***************************
  16. Variable_name: innodb_lru_scan_depth
  17. Value: 1024
  18. *************************** 176. row ***************************
  19. Variable_name: innodb_max_dirty_pages_pct
  20. Value: 75.000000
  21. *************************** 177. row ***************************
  22. Variable_name: innodb_max_dirty_pages_pct_lwm
  23. Value: 0.000000
  24. *************************** 178. row ***************************
  25. Variable_name: innodb_max_purge_lag
  26. Value: 0
  27. ......
  28. mysql> show variables like "%innodb%";
  29. +------------------------------------------+------------------------+
  30. | Variable_name | Value |
  31. +------------------------------------------+------------------------+
  32. | ignore_builtin_innodb | OFF |
  33. | innodb_adaptive_flushing | ON |
  34. | innodb_adaptive_flushing_lwm | 10 |
  35. | innodb_adaptive_hash_index | ON |
  36. | innodb_adaptive_hash_index_parts | 8 |
  37. | innodb_adaptive_max_sleep_delay | 150000 |
  38. ......
  39. ......
  40. | innodb_undo_log_truncate | OFF |
  41. | innodb_undo_logs | 128 |
  42. | innodb_undo_tablespaces | 0 |
  43. | innodb_use_native_aio | ON |
  44. | innodb_version | 5.7.17 |
  45. | innodb_write_io_threads | 4 |
  46. +------------------------------------------+------------------------+
  47. 134 rows in set (0.01 sec)

2)并发连接数量

查看当前已经使用的连接数

  1. mysql> flush status;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> show global status like "Max_used_connections";
  4. +----------------------+-------+
  5. | Variable_name | Value |
  6. +----------------------+-------+
  7. | Max_used_connections | 3 |
  8. +----------------------+-------+
  9. 1 row in set (0.00 sec)

查看默认的最大连接数

  1. mysql> show variables like "max_connections%";
  2. +-----------------+-------+
  3. | Variable_name | Value |
  4. +-----------------+-------+
  5. | max_connections | 151 |
  6. +-----------------+-------+
  7. 1 row in set (0.00 sec)

3)连接超时时间

  1. mysql> show variables like "%timeout%";
  2. +-----------------------------+----------+
  3. | Variable_name | Value |
  4. +-----------------------------+----------+
  5. | connect_timeout | 10 |
  6. | delayed_insert_timeout | 300 |
  7. | have_statement_timeout | YES |
  8. | innodb_flush_log_at_timeout | 1 |
  9. | innodb_lock_wait_timeout | 50 |
  10. | innodb_rollback_on_timeout | OFF |
  11. | interactive_timeout | 28800 |
  12. | lock_wait_timeout | 31536000 |
  13. | net_read_timeout | 30 |
  14. | net_write_timeout | 60 |
  15. | rpl_stop_slave_timeout | 31536000 |
  16. | slave_net_timeout | 60 |
  17. | wait_timeout | 28800 |
  18. +-----------------------------+----------+
  19. 13 rows in set (0.00 sec)

4)允许保存在缓存中被重用的线程数量

  1. mysql> show variables like "thread_cache_size";
  2. +-------------------+-------+
  3. | Variable_name | Value |
  4. +-------------------+-------+
  5. | thread_cache_size | 9 |
  6. +-------------------+-------+
  7. 1 row in set (0.00 sec)

5)用于MyISAM引擎的关键索引缓存大小

  1. mysql> show variables like "key_buffer_size";
  2. +-----------------+---------+
  3. | Variable_name | Value |
  4. +-----------------+---------+
  5. | key_buffer_size | 8388608 |
  6. +-----------------+---------+
  7. 1 row in set (0.00 sec)

6)为每个要排序的线程分配此大小的缓存空间

  1. mysql> show variables like "sort_buffer_size";
  2. +------------------+--------+
  3. | Variable_name | Value |
  4. +------------------+--------+
  5. | sort_buffer_size | 262144 |
  6. +------------------+--------+
  7. 1 row in set (0.00 sec)

7)为顺序读取表记录保留的缓存大小

  1. mysql> show variables like "read_buffer_size";
  2. +------------------+--------+
  3. | Variable_name | Value |
  4. +------------------+--------+
  5. | read_buffer_size | 131072 |
  6. +------------------+--------+
  7. 1 row in set (0.01 sec)

8)为所有线程缓存的打开的表的数量

  1. mysql> show variables like "table_open_cache";
  2. +------------------+-------+
  3. | Variable_name | Value |
  4. +------------------+-------+
  5. | table_open_cache | 2000 |
  6. +------------------+-------+
  7. 1 row in set (0.00 sec)

步骤二:SQL查询优化

1)常用日志种类及选项,如图-1所示:

图-1

记录慢查询,图-2所示:

启用慢查询日志

  1. [root@master10 ~]# vim /etc/my.cnf
  2. ...
  3. slow_query_log=1
  4. slow_query_log_file=mysql-slow.log
  5. long_query_time=5
  6. log_queries_not_using_indexes=1
  7. ...
  8. [root@master10 ~]# systemctl restart mysqld

2)查看慢查询日志

  1. [root@master10 ~]# mysqldumpslow /var/lib/mysql/mysql-slow.log
  2. Reading mysql slow query log from /var/lib/mysql/mysql-slow.log
  3. Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts

查看缓存的大小

  1. mysql> show variables like "query_cache%";
  2. +------------------------------+---------+
  3. | Variable_name | Value |
  4. +------------------------------+---------+
  5. | query_cache_limit | 1048576 |
  6. | query_cache_min_res_unit | 4096 |
  7. | query_cache_size | 1048576 |
  8. | query_cache_type | OFF |
  9. | query_cache_wlock_invalidate | OFF |
  10. +------------------------------+---------+
  11. 5 rows in set (0.00 sec)

3)查看当前的查询缓存统计

  1. mysql> show global status like "qcache%";
  2. +-------------------------+---------+
  3. | Variable_name | Value |
  4. +-------------------------+---------+
  5. | Qcache_free_blocks | 1 |
  6. | Qcache_free_memory | 1031832 |
  7. | Qcache_hits | 0 |
  8. | Qcache_inserts | 0 |
  9. | Qcache_lowmem_prunes | 0 |
  10. | Qcache_not_cached | 40 |
  11. | Qcache_queries_in_cache | 0 |
  12. | Qcache_total_blocks | 1 |
  13. +-------------------------+---------+
  14. 8 rows in set (0.00 sec)

2 案例2:实现MySQL读写分离

2.1 问题

  • 搭建一主一从结构
  • 配置maxscale代理服务器
  • 测试分离配置

2.2 方案

使用4台RHEL 7虚拟机,如图-1所示。其中192.168.4.10和192.168.4.20,分别提供读、写服务,均衡流量,通过主从复制保持数据一致性,由MySQL代理192.168.4.100面向客户端,收到SQL写请求时,交给服务器A处理,收到SQL读请求时,交给服务器B处理。linux客户机用于测试配置,可以使用真机代替

图-1

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:搭建主从

1)搭建一主一从结构,主库192.168.4.10上面操作

  1. [root@master10 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server_id=10    //指定服务器ID号
  4. log-bin=master10        //启用binlog日志,并指定文件名前缀
  5. ...
  6. [root@master10 ~]# systemctl restart mysqld        //重启mysqld

2)从库192.168.4.20上面操作

  1. [mysqld]
  2. server_id=20            //指定服务器ID号,不要与Master的相同
  3. log-bin=slave20        //启动SQL日志,并指定文件名前缀
  4. read_only=1            //只读模式
  5. ...
  6. [root@slave20 ~]# systemctl restart mysqld

3)主库授权一个用户并查看master的状态

  1. [root@master10 ~]# mysql -u root -p123456
  2. mysql> grant all on *.* to 'replicater'@'%' identified by '123456';
  3. Query OK, 0 rows affected, 1 warning (0.00 sec)
  4. mysql> show master status;
  5. +-----------------+----------+--------------+------------------+-------------------+
  6. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  7. +-----------------+----------+--------------+------------------+-------------------+
  8. | master10.000002 | 449 | | | |
  9. +-----------------+----------+--------------+------------------+-------------------+
  10. 1 row in set (0.00 sec)

4)从库通过CHANGE MASTER语句指定MASTER服务器的IP地址、同步用户名/密码、起始日志文件、偏移位置(参考MASTER上的状态输出)

  1. [root@slave20 ~]# mysql -u root -p123456
  2. mysql> change master to master_host='192.168.4.10',
  3. -> master_user='replicater',
  4. -> master_password='123456',
  5. -> master_log_file='master10.000002',
  6. -> master_log_pos=738;
  7. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  8. mysql> start slave;
  9. Query OK, 0 rows affected (0.01 sec)
  10. mysql> show slave status\G;
  11. *************************** 1. row ***************************
  12. Slave_IO_State: Waiting for master to send event
  13. Master_Host: 192.168.4.10
  14. Master_User: replicater
  15. Master_Port: 3306
  16. Connect_Retry: 60
  17. Master_Log_File: master10.000002
  18. Read_Master_Log_Pos: 738
  19. Relay_Log_File: slave20-relay-bin.000002
  20. Relay_Log_Pos: 319
  21. Relay_Master_Log_File: master10.000002
  22. Slave_IO_Running: Yes
  23. Slave_SQL_Running: Yes
  24. Replicate_Do_DB:
  25. Replicate_Ignore_DB:
  26. Replicate_Do_Table:
  27. Replicate_Ignore_Table:
  28. Replicate_Wild_Do_Table:
  29. Replicate_Wild_Ignore_Table:
  30. Last_Errno: 0
  31. Last_Error:
  32. Skip_Counter: 0
  33. Exec_Master_Log_Pos: 738
  34. Relay_Log_Space: 528
  35. Until_Condition: None
  36. Until_Log_File:
  37. Until_Log_Pos: 0
  38. Master_SSL_Allowed: No
  39. Master_SSL_CA_File:
  40. Master_SSL_CA_Path:
  41. Master_SSL_Cert:
  42. Master_SSL_Cipher:
  43. Master_SSL_Key:
  44. Seconds_Behind_Master: 0
  45. Master_SSL_Verify_Server_Cert: No
  46. Last_IO_Errno: 0
  47. Last_IO_Error:
  48. Last_SQL_Errno: 0
  49. Last_SQL_Error:
  50. Replicate_Ignore_Server_Ids:
  51. Master_Server_Id: 10
  52. Master_UUID: 95ada2c2-bb24-11e8-abdb-525400131c0f
  53. Master_Info_File: /var/lib/mysql/master.info
  54. SQL_Delay: 0
  55. SQL_Remaining_Delay: NULL
  56. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  57. Master_Retry_Count: 86400
  58. Master_Bind:
  59. Last_IO_Error_Timestamp:
  60. Last_SQL_Error_Timestamp:
  61. Master_SSL_Crl:
  62. Master_SSL_Crlpath:
  63. Retrieved_Gtid_Set:
  64. Executed_Gtid_Set:
  65. Auto_Position: 0
  66. Replicate_Rewrite_DB:
  67. Channel_Name:
  68. Master_TLS_Version:
  69. 1 row in set (0.00 sec)

5)测试,主库创建aa库

  1. mysql> create database aa;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> show databases;
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | aa |
  9. | mysql |
  10. | performance_schema |
  11. | sys |
  12. +--------------------+
  13. 5 rows in set (0.00 sec)

6)从库上面查看,有aa库

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | aa |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)

步骤二:实现mysql读写分离

1)配置数据读写分离服务器192.168.4.100

环境准备关闭防火墙和SElinux,保证yum源可以正常使用

  1. [root@maxscale ~]# cd mysql/
  2. [root@maxscale mysql]# ls
  3. maxscale-2.1.2-1.rhel.7.x86_64.rpm
  4. [root@maxscale mysql]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm         
  5. //安装maxscale
  6. warning: maxscale-2.1.2-1.rhel.7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 8167ee24: NOKEY
  7. Preparing... ################################# [100%]
  8. Updating / installing...
  9. 1:maxscale-2.1.2-1 ################################# [100%]

2)配置maxscale

  1. [root@maxscale mysql]# vim /etc/maxscale.cnf.template
  2. [maxscale]
  3. threads=auto            //运行的线程的数量
  4. [server1]            //定义数据库服务器
  5. type=server
  6. address=192.168.4.10        //数据库服务器的ip
  7. port=3306
  8. protocol=MySQLBackend        //后端数据库
  9. [server2]
  10. type=server
  11. address=192.168.4.20
  12. port=3306
  13. protocol=MySQLBackend
  14. [MySQL Monitor]                //定义监控的数据库服务器
  15. type=monitor
  16. module=mysqlmon
  17. servers=server1, server2        //监控的数据库列表,不能写ip
  18. user=scalemon                    //监视数据库服务器时连接的用户名scalemon
  19. passwd=123456                    //密码123456
  20. monitor_interval=10000        //监视的频率 单位为秒
  21. #[Read-Only Service]        //不定义只读服务器
  22. #type=service
  23. #router=readconnroute
  24. #servers=server1
  25. #user=myuser
  26. #passwd=mypwd
  27. #router_options=slave
  28. [Read-Write Service]            //定义读写分离服务
  29. type=service
  30. router=readwritesplit
  31. servers=server1, server2
  32. user=maxscaled            //用户名 验证连接代理服务时访问数据库服务器的用户是否存在
  33. passwd=123456                //密码
  34. max_slave_connections=100%
  35. [MaxAdmin Service]        //定义管理服务
  36. type=service
  37. router=cli
  38. #[Read-Only Listener]        //不定义只读服务使用的端口号
  39. #type=listener
  40. #service=Read-Only Service
  41. #protocol=MySQLClient
  42. #port=4008
  43. [Read-Write Listener]            //定义读写服务使用的端口号
  44. type=listener
  45. service=Read-Write Service
  46. protocol=MySQLClient
  47. port=4006
  48. [MaxAdmin Listener]        //管理服务使用的端口号
  49. type=listener
  50. service=MaxAdmin Service
  51. protocol=maxscaled
  52. socket=default
  53. port=4099     //手动添加,不指定时使用的是默认端口在启动服务以后可以知道默认端口是多少

3)根据配置文件的设置,在数据库服务器上添加授权用户(主库执行,从库查看)

  1. mysql> grant replication slave,replication client on *.* to scalemon@'%' identified by "123456";            //监控数据库服务器时,连接数据库服务器的用户
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> grant select on mysql.* to maxscaled@"%" identified by "123456";
  4. //验证 访问数据时,连接数据库服务器使用的用户,是否在数据库服务器上存在的,连接用户
  5. Query OK, 0 rows affected, 1 warning (0.01 sec)

4)查看授权用户

在主库上面查看

  1. mysql> select user,host from mysql.user where user in ("scalemon","maxscaled");
  2. +-----------+------+
  3. | user | host |
  4. +-----------+------+
  5. | maxscaled | % |
  6. | scalemon | % |
  7. +-----------+------+
  8. 2 rows in set (0.00 sec)

在从库上面查看

  1. mysql> select user,host from mysql.user where user in ("scalemon","maxscaled");
  2. +-----------+------+
  3. | user | host |
  4. +-----------+------+
  5. | maxscaled | % |
  6. | scalemon | % |
  7. +-----------+------+
  8. 2 rows in set (0.00 sec)

测试授权用户

  1. [root@maxscale mysql]# mysql -h 192.168.4.10 -u scalemon -p123456
  2. [root@maxscale mysql]# mysql -h 192.168.4.20 -u scalemon -p123456
  3. [root@maxscale mysql]# mysql -h 192.168.4.10 -u maxscaled -p123456
  4. [root@maxscale mysql]# mysql -h 192.168.4.20 -u maxscaled -p123456

5)启动服务

  1. [root@maxscale ~]# maxscale -f /etc/maxscale.cnf
  2. [root@maxscale ~]# ps -C maxscale        //查看进程
  3. PID TTY TIME CMD
  4. 17930 ? 00:00:00 maxscale
  5. [root@maxscale ~]# netstat -antup | grep maxscale //查看端口
  6. tcp6 0 0 :::4099 :::* LISTEN 17930/maxscale
  7. tcp6 0 0 :::4006 :::* LISTEN 17930/maxscale

6)测试,在本机访问管理端口查看监控状态

maxadmin -P端口 -u用户名 -p密码

  1. [root@maxscale ~]# maxadmin -P4099 -uadmin -pmariadb
  2. MaxScale>
  3. MaxScale> list servers
  4. Servers.
  5. -------------------+-----------------+-------+-------------+--------------------
  6. Server | Address | Port | Connections | Status
  7. -------------------+-----------------+-------+-------------+--------------------
  8. server1 | 192.168.4.10 | 3306 | 0 | Master, Running
  9. server2 | 192.168.4.20 | 3306 | 0 | Slave, Running
  10. -------------------+-----------------+-------+-------------+--------------------

7)在客户端访问读写分离服务器(没有mysql命令可以安装)

mysql -h读写分离服务ip -P4006 -u用户名 -p密码

  1. [root@slave53 ~]# mysql -h192.168.4.100 -P4006 -ureplicater -p123456
  2. mysql> select @@hostname;            //查看当前主机名
  3. +------------+
  4. | @@hostname |
  5. +------------+
  6. | slave20 |
  7. +------------+
  8. 1 row in set (0.00 sec)
  9. mysql> create table t2(id int(4) );
  10. Query OK, 0 rows affected (0.02 sec)
  11. mysql> insert into aa.t2 values(777);
  12. Query OK, 1 row affected (0.01 sec)

在主库上面查看

  1. mysql> use aa
  2. mysql> select * from t2;
  3. +------+
  4. | id |
  5. +------+
  6. | 777 |
  7. +------+
  8. 1 row in set (0.00 sec)

从库(主库同步到从库)

  1. mysql> use aa
  2. mysql> select * from t2;
  3. +------+
  4. | id |
  5. +------+
  6. | 777 |
  7. +------+
  8. 1 row in set (0.00 sec)

猜你喜欢

转载自www.cnblogs.com/tiki/p/10783527.html
今日推荐