网络带宽
服务器的硬件配置:查看服务器硬件资源的使用情况CPU 内存存储 I/O
提供数据服务软件版本低。
查看服务运行时的参数设置:/etc/my.cnf
查看变量的值
Showvariables;
Showvariables like “%关键字%”;
mysql>show variables like "%time%";
mysql>show variables like "%max_connections%";
修改变量的值:
命令行修改
Set [global] 变量名=值;
mysql>set max_connections = 500;
ERROR1229 (HY000): Variable 'max_connections' is a GLOBAL variable andshould be set with SET GLOBAL
永久修改
Vim/etc/my.cnf
[mysqld]
常用的参数有哪些
并发连接数
mysql>set global max_connections = 500;
mysql>show variables like "max_connections";
有过的最大连接数量/并发连接数=0.85
Max_used_connections/max_connections=0.85
mysql>show global status like "%conn%";
允许保存在缓存中被重用的线程数量
mysql>show variables like "thread_cache_size";
mysqld ------> t1.* ------> 内存 ----->硬盘
mysql>show variables like "%table%";
为所有线程缓存打开表的数量
mysql>show variables like "table_open_cache";
key_buffer_size索引缓存的大小(从物理内存分配空间以字节为单位)
indexprimary key foreign key
字段排序 存储在 a.MYI /var/lib/mysql/ [root@rootroom9pc01 ~]# echo $[8388608/1024]
8192
[root@rootroom9pc01~]# echo $[8192/1024]
8
8M大小
a.ibd
selectname from user.a where name=”bob”;
帮助文档:
Mysql帮助手册
Mysql配置文件详解
查询缓存的设置
showvariables like “%cache%”;
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 |
+------------------------------+---------+
5rows in set (0.01 sec)
query_cache_type= 0 | 1 | 2
query_cache_size
query_cache_min_res_unit 单位 越小查询越难
query_cache_type=2: select sql_in_cache 字段名列表from表;
query_cache_wlock_invalidateoff
当对mysam存储引擎的表,查询的时候,若此时有客户端对表执行写操作,Mysql服务不会从缓存里找,避免脏读
redis支持数据持久化用内存
showglobal status like “qcache%”;
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 | 0 |
|Qcache_queries_in_cache | 0 |
|Qcache_total_blocks | 1 |
+-------------------------+---------+
8rows in set (0.00 sec)
mysql>
查询缓存统计信息
Qcache_inserts 100
程序员编写的访问数据库服务数据的sql命令复杂导致处理速度。
在数据库服务器上启用慢查询日志,记录超过
binlog日志错误日志
查询日志:记录所有的sql命令
慢查询日志:只记录超过超时时间显示查询结果的sql命令
ls/var/log/mysql.log
vim/etc/my.cnf
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
datadir=/var/lib/mysql
vim/etc/my.cnf
[mysqld]
general-log
[root@host10~]# systemctl restart mysqld
[root@host10~]# cd /var/lib/mysql
[root@host10mysql]# ls *.log
host10.log
[root@host10mysql]# tail -f host10.log
2018-06-26T03:58:59.631502Z 3 Connect root@localhost on using Socket
2018-06-26T03:58:59.632091Z 3 Query select * from mysql.user
2018-06-26T03:59:21.594817Z 3 Query create database t1
慢查询
[root@host10~]# vim /etc/my.cnf
general-log
slow-query-log
long-query-time=
[root@host10~]# !sys
systemctlrestart mysqld
[root@host10~]# ls /var/lib/mysql/
auto.cnf host10-slow.log master51.index public_key.pem
ca-key.pem ib_buffer_pool mysql server-cert.pem
ca.pem ibdata1 mysql.sock server-key.pem
client-cert.pem ib_logfile0 mysql.sock.lock sys
client-key.pem ib_logfile1 performance_schema t1
host10.log ibtmp1 private_key.pem
[root@host10~]# tail -f /var/lib/mysql/host10-slow.log
/usr/sbin/mysqld,Version: 5.7.17-log (MySQL Community Server (GPL)). started with:
Tcpport: 0 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
#Time: 2018-06-26T06:12:07.862030Z
#User@Host: root[root] @ localhost [] Id: 3
#Query_time: 15.010382 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SETtimestamp=1529993527;
selectsleep(15);
[root@host10mysql]# mysqldumpslow host10-slow.log
Readingmysql slow query log from host10-slow.log
Count:1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts
#Time: N-N-26T06:N:N.862030Z
#User@Host: root[root] @ localhost [] Id: N
#Query_time: N.N Lock_time: N.N Rows_sent: N Rows_examined: N
SETtimestamp=N;
selectsleep(N)
[root@host10mysql]# mysqldumpslow host10-slow.log > /root/sql.txt
Readingmysql slow query log from host10-slow.log
[root@host10mysql]# cat /root/sql.txt
Count:1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts
#Time: N-N-26T06:N:N.862030Z
#User@Host: root[root] @ localhost [] Id: N
#Query_time: N.N Lock_time: N.N Rows_sent: N Rows_examined: N
SETtimestamp=N;
selectsleep(N)
[root@host10mysql]#
数据存储架构问题:
数据传输时有瓶颈:
升级硬件CPU 、内存、硬盘
加大网络带宽付费加大带宽
调整mysql 服务运行参数并发连接数、连接超时时间、重复使用的
线程数........
调整与查询相关的参数查询缓存、索引缓存 .......
启用慢查询日志slow-query-log
网络架构不合理调整网络架构
Mysql数据读写分离:
把客户端访问数据库服务时的查询请求和写数据的请求给不同的数据库服务器处理。
读:select
写:insert/delete/update