读写分离的含义:
MySQL读写分离是指让master处理写操作,让slave处理读操作,非常适用于
读操作量比较大的场景,可减轻master的压力。
使用mysql-proxy实现mysql的读写分离,mysql-proxy实际上是作为后
端mysql主从服务器的代理,它直接接受客户端的请求,对SQL语句进行分析,
判断出是读操作还是写操作,然后分发至对应的mysql服务器上。
实验环境:
server1:172.25.38.1 master
server2:172.25.38.2 slave
server3:172.25.38.3 mysql-proxy
实验准备:
可以参考我写的主从复制:https://blog.csdn.net/aaaaaab_/article/details/81515111
第一:在172.25.38.1和172.25.38.2两台mysql搭建主从复制架构。
第二、在proxy机器上安装配置mysql-proxy,实现master/slave架构读写分离。
第一步只演示主从复制效果,详细配置点击上方网址:
在从端可以看到与主端master已经连接:
mysql> show slave status\G 已经达成主从复制
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.254.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> use love;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_love |
+----------------+
| ours |
+----------------+
1 row in set (0.00 sec)
mysql> insert into ours values('xxx','789'); 插入字段信息
Query OK, 1 row affected (0.28 sec)
mysql> use love;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from ours; 可以查看表格信息
+----------+----------+
| username | password |
+----------+----------+
| xfl | 123 |
| xyy | 456 |
| xxx | 789 |
+----------+----------+
3 rows in set (0.00 sec)
[root@server3 ~]# ls
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@server3 ~]# tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz 解压
[root@server3 ~]# ls
mysql-proxy-0.8.5-linux-el6-x86-64bit
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@server3 ~]# cd mysql-proxy-0.8.5-linux-el6-x86-64bit 进入解压路径
[root@server3 mysql-proxy-0.8.5-linux-el6-x86-64bit]# ls
bin include lib libexec licenses share
[root@server3 mysql-proxy-0.8.5-linux-el6-x86-64bit]# cd ..
[root@server3 ~]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy
[root@server3 ~]# cd /usr/local/mysql-proxy/
[root@server3 mysql-proxy]# ls
bin include lib libexec licenses share
[root@server3 mysql-proxy]# mkdir conf
[root@server3 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server3 mysql-proxy]# cd conf/
[root@server3 conf]# ls
[root@server3 conf]# cd ..
[root@server3 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server3 mysql-proxy]# cd conf/
[root@server3 conf]# ls
[root@server3 conf]# cd ..
[root@server3 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server3 mysql-proxy]# cd bin/
[root@server3 bin]# ls
mysql-binlog-dump mysql-myisam-dump mysql-proxy
[root@server3 bin]# pwd
/usr/local/mysql-proxy/bin
[root@server3 bin]# vim ~/.bash_profile 编写环境变量
[root@server3 bin]# source ~/.bash_profile 刷新生效
[root@server3 bin]# ls
mysql-binlog-dump mysql-myisam-dump mysql-proxy
[root@server3 bin]# cd ..
[root@server3 mysql-proxy]# cd conf/
[root@server3 conf]# pwd
/usr/local/mysql-proxy/conf
[root@server3 conf]# ls
mysql-proxy.conf
[root@server3 conf]# cd ..
[root@server3 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server3 mysql-proxy]# cd share/
[root@server3 share]# ls
doc
[root@server3 share]# cd doc/
[root@server3 doc]# ls
mysql-proxy
[root@server3 doc]# cd mysql-proxy/
Lua是一个小巧的脚本语言。是巴西里约热内卢天主教大学里的一个研究小组,
由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo
所组成并于1993年开发。 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。
Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。Lua并没有提供强大的库,
这是由它的定位决定的。所以Lua不适合作为开发独立应用程序的语言。Lua 有一个同时进行的JIT项目,
提供在特定平台上的即时编译功能。
Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。
不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,ini等文件格式,并且更容易理解和维护。Lua由标准C编
写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。一个完整的Lua解释器不过200k,在目前所
有脚本引擎中,Lua的速度是最快的。这一切都决定了Lua是作为嵌入式脚本的最佳选择。
[root@server3 mysql-proxy]# ls lua脚本的默认位置
active-queries.lua ro-balance.lua tutorial-resultset.lua
active-transactions.lua ro-pooling.lua tutorial-rewrite.lua
admin-sql.lua rw-splitting.lua tutorial-routing.lua
analyze-query.lua tutorial-basic.lua tutorial-scramble.lua
auditing.lua tutorial-constants.lua tutorial-states.lua
commit-obfuscator.lua tutorial-inject.lua tutorial-tokenize.lua
commit-obfuscator.msc tutorial-keepalive.lua tutorial-union.lua
COPYING tutorial-monitor.lua tutorial-warnings.lua
histogram.lua tutorial-packets.lua xtab.lua
load-multi.lua tutorial-prep-stmts.lua
README tutorial-query-time.lua
[root@server3 mysql-proxy]# vim rw-splitting.lua
min_idle_connections = 1, ##最小连接数
max_idle_connections = 2, ##最大连接数后实现读写分离
[root@server3 mysql-proxy]# pwd
/usr/local/mysql-proxy/share/doc/mysql-proxy
[root@server3 mysql-proxy]# ll /usr/local/mysql-proxy/share/doc/mysql-proxy
total 288
-rw-r--r-- 1 7161 wheel 3780 Aug 19 2014 active-queries.lua
-rw-r--r-- 1 7161 wheel 2468 Aug 19 2014 active-transactions.lua
-rw-r--r-- 1 7161 wheel 8864 Aug 19 2014 admin-sql.lua
-rw-r--r-- 1 7161 wheel 10816 Aug 19 2014 analyze-query.lua
-rw-r--r-- 1 7161 wheel 1694 Aug 19 2014 auditing.lua
-rw-r--r-- 1 7161 wheel 1683 Aug 19 2014 commit-obfuscator.lua
-rw-r--r-- 1 7161 wheel 186 Aug 19 2014 commit-obfuscator.msc
-rw-r--r-- 1 7161 wheel 18092 Aug 19 2014 COPYING
-rw-r--r-- 1 7161 wheel 5165 Aug 19 2014 histogram.lua
-rw-r--r-- 1 7161 wheel 13282 Aug 19 2014 load-multi.lua
-rw-r--r-- 1 7161 wheel 74230 Aug 19 2014 README
-rw-r--r-- 1 7161 wheel 6646 Aug 19 2014 ro-balance.lua
-rw-r--r-- 1 7161 wheel 7086 Aug 19 2014 ro-pooling.lua
-rw-r--r-- 1 7161 wheel 11341 Aug 14 10:20 rw-splitting.lua
-rw-r--r-- 1 7161 wheel 1334 Aug 19 2014 tutorial-basic.lua
-rw-r--r-- 1 7161 wheel 1473 Aug 19 2014 tutorial-constants.lua
[root@server3 mysql-proxy]# cd ..
[root@server3 doc]# cd ..
[root@server3 share]# cd ..
[root@server3 mysql-proxy]# cd conf/
[root@server3 conf]# ls
mysql-proxy.conf
[root@server3 conf]# vim mysql-proxy.conf
[root@server3 conf]# cd ..
[root@server3 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server3 mysql-proxy]# mkdir logs
[root@server3 mysql-proxy]# cd logs/
[root@server3 logs]# ls
[root@server3 logs]# cd ..
[root@server3 mysql-proxy]# ls
bin conf include lib libexec licenses logs share
[root@server3 mysql-proxy]# cd conf/
[root@server3 conf]# ls
mysql-proxy.conf
[root@server3 conf]# vim mysql-proxy.conf
[root@server3 conf]# vim mysql-proxy.conf
[mysql-proxy]
daemon=true #打入后台
user=root #
keepalive=true
plugins=proxy,admin
log-level=info #日志级别
log-file=/usr/local/mysql-proxy/log/mysql-proxy.log ##proxy日志地址
proxy-address=172.25.38.1:3306 #本机ip地址
proxy-backend-addresses=172.25.38.2:3306 ##backend主 注意addresses
proxy-read-only-backend-addresses=172.25.38.3:3306 ##backend从
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua ##lua脚本地址
admin-address=172.25.28.1:4041 ##proxy的管理用户adminiphe端口
admin-username=admin
admin-password=westos
admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua #admin的lua脚本地址
查看mysql-proxy默认3306端口是否开启:
[root@server3 conf]# chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf
[root@server3 conf]# mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
[root@server3 conf]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 916/redis-server 0.
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 909/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 995/master
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1154/mysql-proxy
tcp 0 0 172.25.38.3:22 172.25.38.250:36438 ESTABLISHED 1043/sshd
tcp 0 0 :::22 :::* LISTEN 909/sshd
tcp 0 0 ::1:25 :::* LISTEN 995/master
[root@server1 ~]# mysql -p
mysql> grant all privileges on *.* to 'root'@'%' identified by 'Yakexi+007';
授予全部权限,但是仅仅是实验环境所与不太在意安全
Query OK, 0 rows affected, 1 warning (0.12 sec)
[root@foundation38 mysql]# mysql -h 172.25.38.3 -u root -p
MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [test]> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| user1 | aaa |
+----------+----------+
命令作用:
lsof(list open files) 是一个列出当前系统打开文件的工具,在linux环境下,任何事物都以
文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。
[root@server1 ~]# yum install -y lsof
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
Package lsof-4.82-4.el6.x86_64 already installed and latest version
Nothing to do
[root@server1 ~]# lsof -i:3306 查找占用端口的线程
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 1739 mysql 30u IPv6 10427 0t0 TCP *:mysql (LISTEN)
mysqld 1739 mysql 61u IPv6 10519 0t0 TCP server1:mysql->server2:43516 (ESTABLISHED)
mysqld 1739 mysql 64u IPv6 11065 0t0 TCP server1:mysql->server3:33359 (ESTABLISHED)
由于文件中配置的最大连接数是2,所以得依次连接超过两个挨个查看效果:
[kiosk@foundation38 images]$ mysql -h 172.25.38.3 -u root -p
MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [test]> select
-> * from linux;
+----------+----------+
| username | password |
+----------+----------+
| user1 | aaa |
+----------+----------+
1 row in set (0.00 sec)
[root@server1 ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 1739 mysql 30u IPv6 10427 0t0 TCP *:mysql (LISTEN)
mysqld 1739 mysql 50u IPv6 11100 0t0 TCP server1:mysql->server3:33360 (ESTABLISHED)
mysqld 1739 mysql 61u IPv6 10519 0t0 TCP server1:mysql->server2:43516 (ESTABLISHED)
mysqld 1739 mysql 64u IPv6 11065 0t0 TCP server1:mysql->server3:33359 (ESTABLISHED)
[kiosk@foundation38 images]$ mysql -h 172.25.38.3 -u root -p
Enter password:
MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [test]> select
-> * from linux;
+----------+----------+
| username | password |
+----------+----------+
| user1 | aaa |
+----------+----------+
1 row in set (0.00 sec)
在master端查看不会变化占用线程,超过两个开始读写分离:
[root@server1 ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 1739 mysql 30u IPv6 10427 0t0 TCP *:mysql (LISTEN)
mysqld 1739 mysql 50u IPv6 11100 0t0 TCP server1:mysql->server3:33360 (ESTABLISHED)
mysqld 1739 mysql 61u IPv6 10519 0t0 TCP server1:mysql->server2:43516 (ESTABLISHED)
mysqld 1739 mysql 64u IPv6 11065 0t0 TCP server1:mysql->server3:33359 (ESTABLISHED)
[root@server2 ~]# yum install lsof
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
Package lsof-4.82-4.el6.x86_64 already installed and latest version
Nothing to do
[root@server2 ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2062 mysql 28u IPv6 10934 0t0 TCP *:mysql (LISTEN)
mysqld 2062 mysql 57u IPv4 10990 0t0 TCP server2:43516->server1:mysql (ESTABLISHED)
mysqld 2062 mysql 68u IPv6 11041 0t0 TCP server2:mysql->server3:52602 (ESTABLISHED)
[kiosk@foundation38 images]$ mysql -h 172.25.38.3 -u root -p
MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [test]> select
-> * from linux;
+----------+----------+
| username | password |
+----------+----------+
| user1 | aaa |
+----------+----------+
1 row in set (0.00 sec)
MySQL [test]> insert into linux values('user2','bbb');
Query OK, 1 row affected (0.15 sec)
在master端可以查看同步意味着写操作直接在master端执行:
[root@server1 ~]# mysql -pAa@+123456
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| user1 | aaa |
| user2 | bbb |
+----------+----------+
2 rows in set (0.00 sec)
[kiosk@foundation38 images]$ mysql -h 172.25.38.3 -u root -p
MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
MySQL [test]> insert into linux values('user3','ccc');
Query OK, 1 row affected (0.08 sec)
在server3进行抓包测试:
[root@server3 conf]# yum install -y tcpdump 安装抓包工具
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
Package 14:tcpdump-4.0.0-3.20090921gitdf3cb4.2.el6.x86_64 already installed and latest version
Nothing to do
[root@server3 conf]# tcpdump -i eth0 port 3306
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:07:43.441155 IP 172.25.38.250.47022 > server3.mysql: Flags [P.], seq 1589502366:1589502410, ack 2704221908, win 245, options [nop,nop,TS val 8220220 ecr 5938391], length 44
11:07:43.441201 IP server3.mysql > 172.25.38.250.47022: Flags [.], ack 44, win 227, options [nop,nop,TS val 6916653 ecr 8220220], length 0
11:07:43.441890 IP server3.33359 > server1.mysql: Flags [P.], seq 34609435:34609479, ack 474288500, win 296, options [nop,nop,TS val 6916654 ecr 4160371], length 44
11:07:43.482200 IP server1.mysql > server3.33359: Flags [.], ack 44, win 227, options [nop,nop,TS val 5320508 ecr 6916654], length 0
11:07:43.518529 IP server1.mysql > server3.33359: Flags [P.], seq 1:12, ack 44, win 227, options [nop,nop,TS val 5320544 ecr 6916654], length 11
11:07:43.518548 IP server3.33359 > server1.mysql: Flags [.], ack 12, win 296, options [nop,nop,TS val 6916730 ecr 5320544], length 0
11:07:43.518698 IP server3.mysql > 172.25.38.250.47022: Flags [P.], seq 1:12, ack 44, win 227, options [nop,nop,TS val 6916730 ecr 8220220], length 11
11:07:43.518765 IP 172.25.38.250.47022 > server3.mysql: Flags [.], ack 12, win 245, options [nop,nop,TS val 8220297 ecr 6916730], length 0
11:07:46.349756 IP 172.25.45.9.39094 > 172.25.45.2.mysql: Flags [P.], seq 1777926393:1777926398, ack 1674443173, win 229, options [nop,nop,TS val 1635833 ecr 5588223], length 5
11:07:46.349766 IP 172.25.45.9.39094 > 172.25.45.2.mysql: Flags [F.], seq 5, ack 1, win 229, options [nop,nop,TS val 1635833 ecr 5588223], length 0
11:11:05.188551 IP 172.25.54.250.36098 > 172.25.54.3.mysql: Flags [P.], seq 1755441761:1755441809, ack 3357259106, win 254, options [nop,nop,TS val 8110978 ecr 5772390], length 48
[root@server3 conf]# vim mysql-proxy.conf
[root@server3 conf]# cat mysql-proxy.conf
[mysql-proxy]
user=root
proxy-address=0.0.0.0:3306
proxy-backend-addresses=172.25.38.1:3306
proxy-read-only-backend-addresses=172.25.38.2:3306
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua
admin-username=admin
admin-password=westos
log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
log-devel=debug
daemon=true
keepalive=true
[root@server3 conf]# mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf --plugins=admin --plugins=proxy
[root@server3 conf]# netstat -antlp 可以看到3306和4041端口
[kiosk@foundation38 images]$ mysql -h 172.25.38.3 -u admin -p -P 4041
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> select * from linux;
ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands
MySQL [(none)]> select * from backends;
+-------------+------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+------------------+---------+------+------+-------------------+
| 1 | 172.25.38.1:3306 | unknown | rw | NULL | 0 |
| 2 | 172.25.38.2:3306 | unknown | ro | NULL | 0 |
+-------------+------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)
测试效果一样依次用真机连接proxy端,当超过两台的时候会开始读写分离,我们可以看到的效果是master端的状态在超过两个时会从状态unknown变成up
MySQL [(none)]> select * from backends;
+-------------+------------------+-------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+------------------+-------+------+------+-------------------+
| 1 | 172.25.38.1:3306 | up | rw | NULL | 0 |
| 2 | 172.25.38.2:3306 | up | ro | NULL | 0 |
+-------------+------------------+-------+------+------+-------------------+
2 rows in set (0.00 sec)
[root@server1 ~]# mysql -p
Enter password:
mysql> show variables like '%slow%'; 数据库的慢查询日志
+---------------------------+---------------------------------+
| Variable_name | Value |
+---------------------------+---------------------------------+
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/server1-slow.log |
+---------------------------+---------------------------------+
5 rows in set (0.00 sec)
mysql> set global slow_query_log=on; 开启查询日志
Query OK, 0 rows affected (0.02 sec)
mysql> show variables like '%slow%'; 再次查数据库的慢查询日志已经打开
+---------------------------+---------------------------------+
| Variable_name | Value |
+---------------------------+---------------------------------+
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/server1-slow.log |
+---------------------------+---------------------------------+
5 rows in set (0.00 sec)
mysql> show variables like '%time%';
+---------------------------------+-------------------+
| Variable_name | Value |
+---------------------------------+-------------------+
| binlog_max_flush_queue_time | 0 |
| connect_timeout | 10 |
| datetime_format | %Y-%m-%d %H:%i:%s |
| default_password_lifetime | 0 |
| delayed_insert_timeout | 300 |
| explicit_defaults_for_timestamp | OFF |
| flush_time | 0 |
| have_statement_timeout | YES |
| innodb_flush_log_at_timeout | 1 |
| innodb_lock_wait_timeout | 50 |
| innodb_old_blocks_time | 1000 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lc_time_names | en_US |
| lock_wait_timeout | 31536000 |
| log_timestamps | UTC |
| long_query_time | 10.000000 |
| max_execution_time | 0 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| rpl_stop_slave_timeout | 31536000 |
| slave_net_timeout | 60 |
| slow_launch_time | 2 |
| system_time_zone | CST |
| time_format | %H:%i:%s |
| time_zone | SYSTEM |
| timestamp | 1534339334.532221 |
| wait_timeout | 28800 |
+---------------------------------+-------------------+
28 rows in set (0.00 sec)
mysql> show variables like 'long%';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
mysql> show variables like 'slow%';
+---------------------+---------------------------------+
| Variable_name | Value |
+---------------------+---------------------------------+
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/server1-slow.log |
+---------------------+---------------------------------+
3 rows in set (0.00 sec)
mysql> select sleep(10);
+-----------+
| sleep(10) |
+-----------+
| 0 |
+-----------+
1 row in set (10.00 sec)
mysql> quit
Bye
[root@server1 ~]# cd /var/lib/mysql
[root@server1 mysql]# ls
auto.cnf ib_logfile0 mysql-bin.000003 private_key.pem
ca-key.pem ib_logfile1 mysql-bin.000004 public_key.pem
ca.pem ibtmp1 mysql-bin.index server1-slow.log
client-cert.pem love mysqld_safe.pid server-cert.pem
client-key.pem mysql mysql.sock server-key.pem
ib_buffer_pool mysql-bin.000001 mysql.sock.lock sys
ibdata1 mysql-bin.000002 performance_schema test
[root@server1 mysql]# cat server1-slow.log
/usr/sbin/mysqld, Version: 5.7.17-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 2018-08-15T13:23:07.486653Z
# User@Host: root[root] @ localhost [] Id: 4
# Query_time: 10.000479 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1534339387;
select sleep(10);
[root@server1 mysql]# mysql
mysql mysqlimport
mysqladmin mysql_install_db
mysqlbinlog mysql_plugin
mysqlcheck mysqlpump
mysql_config_editor mysql_secure_installation
mysqld mysqlshow
mysqld-debug mysqlslap
mysqld_multi mysql_ssl_rsa_setup
mysqld_safe mysql_tzinfo_to_sql
mysqldump mysql_upgrade
mysqldumpslow
[root@server1 mysql]# mysqldumpslow -s t server1-slow.log
Reading mysql slow query log from server1-slow.log
Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts
# Time: N-N-15T13:N:N.486653Z
# User@Host: root[root] @ localhost [] Id: N
# Query_time: N.N Lock_time: N.N Rows_sent: N Rows_examined: N
SET timestamp=N;
select sleep(N)
[root@server1 mysql]# ls
auto.cnf ib_logfile0 mysql-bin.000003 private_key.pem
ca-key.pem ib_logfile1 mysql-bin.000004 public_key.pem
ca.pem ibtmp1 mysql-bin.index server1-slow.log
client-cert.pem love mysqld_safe.pid server-cert.pem
client-key.pem mysql mysql.sock server-key.pem
ib_buffer_pool mysql-bin.000001 mysql.sock.lock sys
ibdata1 mysql-bin.000002 performance_schema test
[root@server1 mysql]# cat server1-slow.log
/usr/sbin/mysqld, Version: 5.7.17-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 2018-08-15T13:23:07.486653Z
# User@Host: root[root] @ localhost [] Id: 4
# Query_time: 10.000479 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1534339387;
select sleep(10);
[root@server1 mysql]# mysql -p
mysql> select sleep(10);
+-----------+
| sleep(10) |
+-----------+
| 0 |
+-----------+
1 row in set (10.00 sec)
mysql> show variables like '%format%';
+---------------------------+-------------------+
| Variable_name | Value |
+---------------------------+-------------------+
| binlog_format | ROW |
| date_format | %Y-%m-%d |
| datetime_format | %Y-%m-%d %H:%i:%s |
| default_week_format | 0 |
| innodb_default_row_format | dynamic |
| innodb_file_format | Barracuda |
| innodb_file_format_check | ON |
| innodb_file_format_max | Barracuda |
| time_format | %H:%i:%s |
+---------------------------+-------------------+
9 rows in set (0.00 sec)
mysql> show variables like '%size%';
+----------------------------------------------------------+----------------------+
| Variable_name | Value |
+----------------------------------------------------------+----------------------+
| binlog_cache_size | 32768 |
| binlog_stmt_cache_size | 32768 |
| bulk_insert_buffer_size | 8388608 |
| delayed_queue_size | 1000 |
| host_cache_size | 279 |
| innodb_buffer_pool_chunk_size | 134217728 |
| innodb_buffer_pool_size | 134217728 |
| innodb_change_buffer_max_size | 25 |
| innodb_ft_cache_size | 8000000 |
| innodb_ft_max_token_size | 84 |
| innodb_ft_min_token_size | 3 |
| innodb_ft_total_cache_size | 640000000 |
| innodb_log_buffer_size | 16777216 |
| innodb_log_file_size | 50331648 |
| innodb_log_write_ahead_size | 8192 |
| innodb_max_undo_log_size | 1073741824 |
| innodb_online_alter_log_max_size | 134217728 |
| innodb_page_size | 16384 |
| innodb_purge_batch_size | 300 |
| innodb_sort_buffer_size | 1048576 |
| innodb_sync_array_size | 1 |
| join_buffer_size | 262144 |
| key_buffer_size | 8388608 |
| key_cache_block_size | 1024 |
| large_page_size | 0 |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| max_heap_table_size | 16777216 |
| max_join_size | 18446744073709551615 |
| max_relay_log_size | 0 |
| metadata_locks_cache_size | 1024 |
| myisam_data_pointer_size | 6 |
| myisam_max_sort_file_size | 9223372036853727232 |
| myisam_mmap_size | 18446744073709551615 |
| myisam_sort_buffer_size | 8388608 |
| ngram_token_size | 2 |
| optimizer_trace_max_mem_size | 16384 |
| parser_max_mem_size | 18446744073709551615 |
| performance_schema_accounts_size | -1 |
| performance_schema_digests_size | 10000 |
| performance_schema_events_stages_history_long_size | 10000 |
| performance_schema_events_stages_history_size | 10 |
| performance_schema_events_statements_history_long_size | 10000 |
| performance_schema_events_statements_history_size | 10 |
| performance_schema_events_transactions_history_long_size | 10000 |
| performance_schema_events_transactions_history_size | 10 |
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size | 10 |
| performance_schema_hosts_size | -1 |
| performance_schema_session_connect_attrs_size | 512 |
| performance_schema_setup_actors_size | -1 |
| performance_schema_setup_objects_size | -1 |
| performance_schema_users_size | -1 |
| preload_buffer_size | 32768 |
| profiling_history_size | 15 |
| query_alloc_block_size | 8192 |
| query_cache_size | 1048576 |
| query_prealloc_size | 8192 |
| range_alloc_block_size | 4096 |
| range_optimizer_max_mem_size | 8388608 |
| read_buffer_size | 131072 |
| read_rnd_buffer_size | 262144 |
| slave_pending_jobs_size_max | 16777216 |
| sort_buffer_size | 262144 |
| thread_cache_size | 9 |
| tmp_table_size | 16777216 |
| transaction_alloc_block_size | 8192 |
| transaction_prealloc_size | 4096 |
+----------------------------------------------------------+----------------------+
69 rows in set (0.00 sec)
mysql> show variables like '%day%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| expire_logs_days | 0 |
+------------------+-------+
1 row in set (0.00 sec)