ubuntu下使用docker运行mysql,连接查询耗时太长

ubuntu下使用docker运行mysql,连接查询耗时太长

    • 1、docker运行mysql(数据存储目录映射到宿主机)
    • 2、docker运行mysql(不做目录映射)
    • 3、优化做了目录映射共享的mysql容器
      • 3.1、优化过程
      • 3.2、优化验证

1、docker运行mysql(数据存储目录映射到宿主机)

  • 运行容器:docker run -id --name=mysql5.7.24 -p 12306:3306 -v /usr/local/docker-dir/mysql2/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=yourpassword --restart always mysql:5.7.24 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
ffa8a1c592f407a780d71f0a042248f1a89d31ca12c2e9a263e67842a83a2ac7
  • 查看容器IP:docker inspect --format=’{{.NetworkSettings.IPAddress}}’ mysql5.7.24
172.17.0.3
  • 模拟并发测试性能:mysqlslap -a -c 50,100 --number-of-queries 10000 -i 10 -h172.17.0.3 -uroot -pyourpassword
Benchmark
	Average number of seconds to run all queries: 31.633 seconds
	Minimum number of seconds to run all queries: 31.269 seconds
	Maximum number of seconds to run all queries: 32.194 seconds
	Number of clients running queries: 50
	Average number of queries per client: 200

Benchmark
	Average number of seconds to run all queries: 27.778 seconds
	Minimum number of seconds to run all queries: 27.436 seconds
	Maximum number of seconds to run all queries: 28.119 seconds
	Number of clients running queries: 100
	Average number of queries per client: 100

2、docker运行mysql(不做目录映射)

  • 运行容器:docker run -di --name=mysql2 -p 12307:3306 -e MYSQL_ROOT_PASSWORD=yourpassword --restart always mysql:5.7.24 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
71cddcbfe93da9168f1922de41347da846cc59c5cf1b1240b58e59159492dfba
  • 查看容器IP:docker inspect --format=’{{.NetworkSettings.IPAddress}}’ mysql5.7.24
172.17.0.7
  • 模拟并发测试性能:mysqlslap -a -c 50,100 --number-of-queries 10000 -i 10 -h172.17.0.7 -uroot -pyourpassword
Benchmark
	Average number of seconds to run all queries: 11.222 seconds
	Minimum number of seconds to run all queries: 10.831 seconds
	Maximum number of seconds to run all queries: 11.577 seconds
	Number of clients running queries: 50
	Average number of queries per client: 200

Benchmark
	Average number of seconds to run all queries: 7.164 seconds
	Minimum number of seconds to run all queries: 6.961 seconds
	Maximum number of seconds to run all queries: 7.522 seconds
	Number of clients running queries: 100
	Average number of queries per client: 100

3、优化做了目录映射共享的mysql容器

3.1、优化过程

  • 查看磁盘管理配置文件:cat /etc/fstab
#                
# / was on /dev/sdb2 during installation
UUID=d91c81a0-74ed-4b1d-b5d3-e8862add46d1 /   ext4   errors=remount-ro 0  1
# swap was on /dev/sdb3 during installation
UUID=0f106376-1f87-4c53-865c-01688b4f72da none     swap    sw    0   0
  • 参考:ubuntu mount - 挂载文件系统;EXT4快速运行MySQL
barrier=none / barrier=flush
              This disables /  enables  the  use  of  write  barriers  in  the  journaling  code.
              barrier=none  disables,  barrier=flush enables (default).  This also requires an IO
              stack which can support barriers, and if reiserfs gets an error on a barrier write,
              it  will  disable barriers again with a warning.  Write barriers enforce proper on-
              disk ordering of journal commits, making volatile disk write caches safe to use, at
              some  performance penalty.  If your disks are battery-backed in one way or another,
              disabling barriers may safely improve performance.
  • barrier写屏障可提高安全使用,但会造成一些性能损失。如果磁盘以某种方式进行了备份可禁用屏障提高性能:配置/etc/fstab中etx4的设置barrier= 0 ;保存修改;重启!
UUID=d91c81a0-74ed-4b1d-b5d3-e8862add46d1 /   ext4   errors=remount-ro,barrier= 0 0  1
UUID=0f106376-1f87-4c53-865c-01688b4f72da none     swap    sw    0   0

3.2、优化验证

  • 再次执行容器并发测试,耗时依旧过长;
  • 停止并删除原容器,重新运行,并发测试,耗时减少,性能提升(注:当前仅共享数据存储目录;若需要同时对日志和配置文件共享,需留意宿主机覆盖问题)。
Benchmark
	Average number of seconds to run all queries: 10.431 seconds
	Minimum number of seconds to run all queries: 9.800 seconds
	Maximum number of seconds to run all queries: 11.022 seconds
	Number of clients running queries: 50
	Average number of queries per client: 200

Benchmark
	Average number of seconds to run all queries: 6.848 seconds
	Minimum number of seconds to run all queries: 6.563 seconds
	Maximum number of seconds to run all queries: 7.196 seconds
	Number of clients running queries: 100
	Average number of queries per client: 100

你可能感兴趣的:(docker,docker,mysql,查询耗时太长)