1、从硬件上优化(mysql服务器尽量不要联外网)
硬盘:选用SAS硬盘(15000转)做RAID10,如果公司有钱的话可以使用SSD硬盘
cpu:推荐DELL R710 (双四核16线程或单八核16线程)
内存:推荐32G
2、配置文件优化(参考)
vi /etc/my.cnf
#这是一份DELL R720 32G,RAID10 的参数
[client]
default-character-set = utf8 #设置客户端字符集
[mysqld] #一定要在【mysqld】下面
user=mysql #启动mysql进程的运行用户
character-set-server = utf8 #服务端字符集,mysql5.1及以前用这个字符集 #default-character-set = utf8
socket=/var/lib/mysql/mysql.sock #服务器与本地客户端进行通信的Unix套接字文件,如果是因为这个文件报错起不来主要看下配置文件是否错误
datadir=/Data/mysql #存放mysql数据库的主要数据目录,需要先创建并chown授权mysql用户
slave-skip-errors=1062,1053,1146 #跳过常见的指定error no类型的错误,这个要在从库上配置,详细见最后
server-id = 10 #用来指定哪个是主库哪个是从库,和用来标识是哪个服务器
log-bin=mysql-bin #开启binlog日志,和server-id一起存在
expire_logs_days = 7 #只保留7天bin-log日志
binlog_format=mixed #mysql复制方法:混合
innodb_buffer_pool_size = 8000M #当innodb表的索引或数据达到8G的时候再将数据写入磁盘,这个值设为内存的50%左右(否则会造成磁盘IO过高)
max_allowed_packet = 32M #有时候大的插入和更新会被max_allowed_packet 参数限制掉,导致失败。 或报错[MySQL server has gone away]
key_buffer_size = 2000M #指定用于索引的缓冲区大小,增加它可得到更好的索引处理性能。对于内存在4GB左右的服务器来说,该参数可设置为256MB或384MB。【内存*0.0625】
symbolic-links=0
wait_timeout=1000
interactive_timeout=1000
max-connect-errors=100000
max-connections=5000
table_open_cache = 256
sort_buffer_size = 1024K
net_buffer_length = 32K
read_buffer_size = 512K
read_rnd_buffer_size = 1024K
myisam_sort_buffer_size = 16M
slow_query_log=1
thread_cache_size=32
local-infile=0
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-character-set-client-handshake = 1
skip-name-resolve
[mysqldump]
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysql]
[mysqld_safe]
log-error=/var/log/mysqld.log #指定mysql错误日志
pid-file=/var/run/mysqld/mysqld.pid #mysql pid文件, pid 文件记录的是当前 mysqld 进程的 pid,pid 亦即 Process ID
3、权限控制优化(root用户是超级用户)
(1) 删除没用的账户
mysql> select user,host,password from mysql.user; 或下面的命令
#mysql> sELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+-----------+-------------+
| user | host |
+-----------+-------------+
| root | 127.0.0.1 | #不要删
| root | 172.16.1.% |
| wordpress | 172.16.1.% |
| wordpress | 172.16.1.51 |
| root | 172.16.1.7 |
| wenqiang | 172.16.1.7 |
| root | ::1 |
| | oldboy |
| root | localhost | #不要删
+-----------+-------------+
9 rows in set (0.00 sec)
mysql> drop user wenqiang@'172.16.1.%';
(2) 授权用户时尽量不要使用%,比如:(允许本地root用户访问所有数据库并拥有所有权限)
授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
mysql> grant all on *.* to root@'%' identified by '123456';
改为:
mysql> grant all on *.* to root@'10.0.0.10' identified by '123456';
4、案例
(1)某动漫公司线上配置文件(RAID10、内存32GB、DELL R710)
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8
[mysqld]
port = 3306
socket = /tmp/mysql.sock
wait_timeout=1000
interactive_timeout=1000
max-connect-errors=100000
max-connections=2000
innodb_flush_method=O_DIRECT
skip-name-resolve
innodb_buffer_pool_size = 8000M
innodb_additional_mem_pool_size = 16M
innodb_log_buffer_size = 64M
innodb_log_file_size = 256M
skip-external-locking
key_buffer_size = 2000M
max_allowed_packet = 16M
table_open_cache = 256
sort_buffer_size = 1024K
net_buffer_length = 32K
read_buffer_size = 512K
read_rnd_buffer_size = 1024K
myisam_sort_buffer_size = 16M
slow_query_log=1
thread_cache_size=32
long_query_time=2
local-infile=0
log-bin=mysql-bin
binlog_format=mixed
server-id = 10
character_set_server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-character-set-client-handshake = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
(2) 某电商公司的数据库配置文件
电商MySQL数据库配置文件
这是一份电子商务网站MySQL数据库调整后所运行的配置文件/etc/my.cnf(服务器为DELL R710、16GB内存、RAID10),大家可以根据实际的MySQL数据库硬件情况进行调整配置文件如下:
[client]
port = 3306
socket = /data/3306/mysql.sock
default-character-set = utf8
[mysqld]
user = mysql
port = 3306
character-set-server = utf8
socket = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/3306/data
log-error=/data/3306/mysql_err.log
pid-file=/data/3306/mysql.pid
log_slave_updates = 1
log-bin = /data/3306/mysql-bin
binlog_format = mixed
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 1G
expire_logs_days = 90
binlog-ignore - db = mysql
binlog-ignore - db = information_schema
key_buffer_size = 384M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
join_buffer_size = 2M
thread_cache_size = 8
query_cache_size = 32M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
thread_concurrency = 32
table_cache = 614
table_open_cache = 512
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
external-locking = FALSE
max_allowed_packet =16M
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 256M
max_heap_table_size = 512M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
long_query_time = 2
slow_query_log
slow_query_log_file = /data/3306/slow.log
skip-name-resolv
skip-locking
skip-networking
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 64M
[mysql]
no – auto - rehash