[root@master1 ~]# service mysql stop //停止master1上服务
Shutting down MySQL.... [确定]
[root@master1 ~]#
[root@monitor ~]# mmm_control show //查看集群内服务器状态
通过输出信息可以看到虚拟IP从master1切换到master2:
master1(192.168.4.10) master/HARD_OFFLINE. Roles:
master2(192.168.4.11) master/ONLINE. Roles: writer(192.168.4.200)
slave1(192.168.4.12) slave/ONLINE. Roles: reader(192.168.4.201)
slave2(192.168.4.13) slave/ONLINE. Roles: reader(192.168.4.202)
[root@monitor ~]#
[root@client ~]# mysql -h192.168.4.200 -uroot -ppwd123 -e "show databases" //访问虚拟IP测试
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tarena |
| test |
+--------------------+
[root@client ~]#
3 MySQL性能调优
3.1 问题
基于一台普通版的MySQL服务器,执行下列操作:
练习my.cnf配置相关选项
启用慢查询日志
查看各种系统变量、状态变量
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:MySQL并发及连接控制
max_connections对应并发客户端连接的数量,增加该值会增加 mysqld 要求的文件描述符的数量。若这个数值太小,可能会经常出现“too many connections”错误。比如 默认的数值是151,可以将其改为1024。
1)查看当前已建立的连接数
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 | 5 |
+----------------------+-------+
1 row in set (0.05 sec)
2)查看当前的最大连接数限制
mysql> SHOW VARIABLES LIKE 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
步骤二:MySQL缓存参数控制
当 Key_reads / Key_read_requests 较低时,可适当加大key_buffer_size的缓存值,以提高性能。而增大sort_buffer_size的值,可以显著提高ORDER和GROUP的响应速度。
1)查看key_read相关数值
mysql> SHOW GLOBAL STATUS LIKE 'key_read%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Key_read_requests | 0 |
| Key_reads | 0 |
+-------------------+-------+
2 rows in set (0.00 sec)
2)查看当前的key_buffer_size缓存大小
mysql> SHOW VARIABLES LIKE 'key_buffer_size';
+-----------------+---------+
| Variable_name | Value |
+-----------------+---------+
| key_buffer_size | 8388608 |
+-----------------+---------+
1 row in set (0.03 sec)
3)查看当前的sort_buffer_size大小
mysql> SHOW VARIABLES LIKE 'sort_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| sort_buffer_size | 262144 |
+------------------+--------+
1 row in set (0.00 sec)
4)查看检索表记录时的读取缓存大小
缓存值read_buffer_size和read_rnd_buffer_size会影响SQL查询的响应速度: