利用mysqldump实现mysql数据库全量备份和增量备份脚本

mysqldump实现全量备份,增量备份脚本,

增量备份,其实就是复制日志

全备脚本:
#! /bin/bash
# mysql 全量备份脚本,建议在slave上运行,并开启log_slave_updates=1
mkdir /backup
cd /backup
datadir=`date +”%y-%m-%d”`
mkdir -p $datadir/data
path=/usr/local/mysql/data
for i in `mysql -uroot -p123456 -e “show databases” |grep -v “Database”`
do
mysqldump -uroot -p123456 –defaultes-character-set=utf8 -q –lock-all-tables –flush-logs -E -R –triggers -B $i |gzip > /backup/$datadir/data/${i}_${datadir}.sql.gz
#mysqldump -uroot -ptiancity –opt –single-transaction –flush-logs -E -R -C –triggers $i |gzip > “/backup/$datadir/data/${i}_${datadir}.sql.gz”
done
binlog_rm=`tail -n 1 /app/mysql/data/mysql-bin.index |sed ‘s/\/app\/mysql\/data\///’`
mysql -uroot -p123456 -e “purge binary logs to ‘$binlog_rm'”

增备脚本:
#! /bin/bash
# mysql 增量备份脚本,其实就是复制二进制日志,
cd /backup
datadir=`date +”%y-%m-%d”`
mkdir -p $datadir/data
path=/usr/local/mysql/data
mysqladmin -uroot -p12334 flush-logs
# mysql -uroot -p12334 -e “flush logs”
binlog_cp=`head -n -1 $path/mysql-bin.index |sed ‘s/\/app\/mysql\/data\///’`
for i in $binlog_cp
do
cp $path/$i /backup/$datadir/data/
done
binlog_rm=`tail -n 1 /app/mysql/data/mysql-bin.index |sed ‘s/\/app\/mysql\/data\///’`
mysql -uroot -p12334 -e “purge binary logs to ‘$binlog_rm'”

mysql数据库参数文件性能调试工具:tuning-primer.sh

个人感觉,这脚本非常不错,可以很直观的发现,参数配置上的问题:
下载地址为:http://www.day32.com/MySQL/tuning-primer.sh 
 
下面安装,演示一下:
1.安装的话,如果能连外网,直接使用如下命令(不能连接外网,就先下来再使用)
 [root@10-9-7-79 test]#  wget http://www.day32.com/MySQL/tuning-primer.sh
2.给予脚本执行权限:
[root@10-9-7-79 test]# chmod 755 tuning-primer.sh
3.执行tuning-primer.sh 脚本:
[root@10-9-7-79 test]# ./tuning-primer.sh
4.下面就是测试结果:[测试结果内容,主要看每个测试结果的最后几行,特别注意内存这块,这块很容易就配置超出内存]
Using login values from ~/.my.cnf
– INITIAL LOGIN ATTEMPT FAILED –
Testing for stored webmin passwords:
None Found
Could not auto detect login info!
Found potential sockets: /tmp/mysql.sock
Using: /tmp/mysql.sock
# 以下这几项需要输入的,相信各位都能看得懂,就不解释
Would you like to provide a different socket?: [y/N] n
Do you have your login handy ? [y/N] : y
User: root
Password: 122345

Would you like me to create a ~/.my.cnf file for you? [y/N] : n

— MYSQL PERFORMANCE TUNING PRIMER —
– By: Matthew Montgomery –

MySQL Version 5.5.37-log x86_64

Uptime = 5 days 4 hrs 11 min 46 sec
Avg. qps = 0
Total Questions = 13261
Threads Connected = 1

Server has been running for over 48hrs.
It should be safe to follow these recommendations

To find out more information on how each of these
runtime variables effects performance visit:
http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html
Visit http://www.mysql.com/products/enterprise/advisors.html
for info about MySQL’s Enterprise Monitoring and Advisory Service

SLOW QUERIES
The slow query log is enabled.
Current long_query_time = 1.000000 sec.
You have 1533 out of 13282 that take longer than 1.000000 sec. to complete
Your long_query_time seems to be fine

BINARY UPDATE LOG
The binary update log is enabled
Binlog sync is not enabled, you could loose binlog records during a server crash

WORKER THREADS
Current thread_cache_size = 512
Current threads_cached = 5
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine

MAX CONNECTIONS
Current max_connections = 1000
Current threads_connected = 1
Historic max_used_connections = 6
The number of used connections is 0% of the configured maximum.
You are using less than 10% of your configured max_connections.
Lowering max_connections could help to avoid an over-allocation of memory
See “MEMORY USAGE” section to make sure you are not over-allocating

INNODB STATUS
Current InnoDB index space = 160 M
Current InnoDB data space = 2.10 G
Current InnoDB buffer pool free = 46 %
Current innodb_buffer_pool_size = 4.00 G
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory

MEMORY USAGE
Max Memory Ever Allocated : 4.10 G
Configured Max Per-thread Buffers : 2.31 G
Configured Max Global Buffers : 4.09 G
Configured Max Memory Limit : 6.41 G
Physical Memory : 7.62 G
Max memory limit seem to be within acceptable norms

KEY BUFFER
Current MyISAM index space = 109 K
Current key_buffer_size = 64 M
Key cache miss rate is 1 : 5
Key buffer free ratio = 81 %
Your key_buffer_size seems to be fine

QUERY CACHE
Query cache is enabled
Current query_cache_size = 2 M
Current query_cache_used = 16 K
Current query_cache_limit = 1 M
Current Query cache Memory fill ratio = .82 %
Current query_cache_min_res_unit = 4 K
Your query_cache_size seems to be too high.
Perhaps you can use these resources elsewhere
MySQL won’t cache query results that are larger than query_cache_limit in size

SORT OPERATIONS
Current sort_buffer_size = 256 K
Current read_rnd_buffer_size = 256 K
Sort buffer seems to be fine

JOINS
Current join_buffer_size = 132.00 K
You have had 0 queries where a join could not use an index properly
Your joins seem to be using indexes properly

OPEN FILES LIMIT
Current open_files_limit = 65535 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
Your open_files_limit value seems to be fine

TABLE CACHE
Current table_open_cache = 4096 tables
Current table_definition_cache = 4096 tables
You have a total of 183 tables
You have 184 open tables.
The table_cache value seems to be fine

TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 16 M
Of 3294 temp tables, 38% were created on disk
Perhaps you should increase your tmp_table_size and/or max_heap_table_size
to reduce the number of disk-based temporary tables
Note! BLOB and TEXT columns are not allow in memory tables.
If you are using these columns raising these values might not impact your
ratio of on disk temp tables.

TABLE SCANS
Current read_buffer_size = 512 K
Current table scan ratio = 9703 : 1
You have a high ratio of sequential access requests to SELECTs
You may benefit from raising read_buffer_size and/or improving your use of indexes.

TABLE LOCKING
Current Lock Wait ratio = 0 : 13543
Your table locking seems to be fine

mysql优化可以考虑的21个方面

在日常朋友间交流中,总会谈论数据库的优化问题,下面为转载的一片文章,优化方面说的还是蛮详细的。

细节东西(table cache, 表设计,索引设计,程序端缓存之类的)先不赘述。

1. 要确保有足够的内存

数据库能够高效的运行,最关建的因素需要内存足够大,能缓存住数据,更新也可以在内存先完成。但不同的业务对内存需要强度不一样,推荐内存要占到数据的15-25%的比例,特别的热的数据,内存基本要达到数据库的80%大小。

2. 需要更多更快的CPU

MySQL 5.6可以利用到64个核,而MySQL每个query只能运行在一个CPU上,所以要求更多的CPU,更快的CPU会更有利于并发。

3. 要选择合适的操作系统

在官方建议估计最推荐的是Solaris, 从实际生产中看CentOS, REHL都是不错的选择,推荐使用CentOS, REHL 版本为6以后的,当然Oracle Linux也是一个不错的选择。虽然从MySQL 5.5后对Windows做了优化,但也不推荐在高并发环境中使用windows.

4. 合理的优化系统的参数

更改文件句柄 ulimit –n 默认1024 太小

进程数限制 ulimit –u 不同版本不一样

禁掉NUMA numctl –interleave=all

修改方法:vim /etc/profile ==> 添加 ulimit -HSn 65535 ,然后 source /etc/profile

5. 选择合适的内存分配算法

默认的内存分配就是c的malloc 现在也出现许多优化的内存分配算法:

jemalloc and tcmalloc

从MySQL 5.5后支持声明内存储方法。

[mysqld_safe]

malloc-lib = tcmalloc

或是直接指到so文件

[mysqld_safe]

malloc-lib=/usr/local/lib/libtcmalloc_minimal.so

6. 使用更快的存储设备ssd或是固态卡

存储介质十分影响MySQL的随机读取,写入更新速度。新一代存储设备固态ssd及固态卡的出现也让MySQL 大放异彩。

7. 选择良好的文件系统推荐XFS, Ext4

如果还在使用ext2,ext3的同学请尽快升级别。推荐XFS,这个也是今后一段时间Linux会支持一个文件系统。

文件系统强烈推荐: XFS

查看方式为: df -Th

8. 优化挂载文件系统的参数

挂载XFS参数:(rw, noatime,nodiratime,nobarrier)

挂载ext4参数:ext4 (rw,noatime,nodiratime,nobarrier,data=ordered)

如果使用SSD或是固态盘需要考虑:

innodb_page_size = 4K

Innodb_flush_neighbors = 0

9. 选择适合的IO调度

正常请下请使用deadline 默认是noop

echo dealine > /sys/block/{DEV-NAME}/queue/scheduler

10. 选择合适的Raid卡

Cache策略请使用带电的Raid,启用WriteBack,对于加速redo log ,binary log, data file都有好处。

11. 禁用Query Cache

Query Cache在Innodb中有点鸡肋,Innodb的数据本身可以在Innodb buffer pool中缓存,Query Cache属于结果集缓存,如果开启Query Cache更新写入都要去检查query cache反而增加了写入的开销。
在MySQL 5.6中Query cache是被禁掉了。

12. 使用Thread Pool

现在一个数据对应5个以上App场景比较,但MySQL有个特性随着连接增多的情况下性能反而下降,所以对于连接超过200的以后场景请考虑使用thread pool. 这是一个伟大的发明。

这个mysql 5.5中提供,但仅是商业版提供,所以建议使用mariadb

13. 合理调整内存

(1). 减少连接的内存分配,连接可以用thread_cache_size缓存,观察不如thread pool给力。数据库在连上分配的内存如下:

max_used_connections * (read_buffer_size +read_rnd_buffer_size +join_buffer_size +sort_buffer_size +binlog_cache_size +thread_stack +2 * net_buffer_length …)

(2). 使较大的buffer pool要把60-80%的内存分给innodb_buffer_pool_size. 这个不要超过数据大小了,另外也不要分配超过80%,不然会利用到swap.

14. 合理选择LOG刷新机制Redo Logs:

– innodb_flush_log_at_trx_commit = 1 // 最安全

– innodb_flush_log_at_trx_commit = 2 //  较好性能

– innodb_flush_log_at_trx_commit = 0 //  最好的情能
binlog :

sync_binlog= 1 需要group commit支持,如果没这个功能可以考虑binlog_sync=0来获得较佳性能。
关于组提交实现的前提条件是:应该是:【 sync_binlog=0,innodb_support_xa=0】,不知道理解对否?大家可以发表想法.

数据文件:innodb_flush_method = O_DIRECT

15. 请使用Innodb表,以利用更多资源,在线alter操作有所提高

目前也支持非中文的full text(mysql 5.6支持), 同时支持Memcache API访问。目前也是MySQL最优秀的一个引擎。如果你还在MyISAM请考虑快速转换。

16. 设置较大的Redo log

以前Percona 5.5和官方MySQL 5.5比拼性能时,胜出的一个Tips就是分配了超过4G的Redo log ,而官方MySQL5.5 redo log不能超过4G. 从 MySQL 5.6后可以超过4G了,通常建Redo log加起来要超过500M。 可以通过观查redo log产生量,分配Redo log大于一小时的量即可。

17. 优化磁盘的IO

innodb_io_capactiy 在sas 15000转的下配置800就可以了,在ssd下面配置2000以上。
在MySQL 5.6:

innodb_lru_scan_depth = innodb_io_capacity / innodb_buffer_pool_instances

innodb_io_capacity_max = min(2000, 2 * innodb_io_capacity)

18. 使用独立表空间

目前来看新的特性都是独立表空间支持:

truncate table 表空间回收,表空间传输,较好的去优化碎片等管理性能的增加,整体上来看使用独立表空间是没用的。

19. 配置合理的并发

innodb_thread_concurrency =并发数

这个参数在Innodb中变化也是最频繁的一个参数。不同的版本,有可能不同的小版本也有变动。一般推荐:

在使用thread pool 的情况下:

innodb_thread_concurrency = 0 就可以了。

如果在没有thread pool的情况下:

5.5 推荐:innodb_thread_concurrency =16 – 32

5.6 推荐:innodb_thread_concurrency = 36

20. 优化事务隔离级别

默认是 Repeatable read

推荐使用Read committed: 这个观点,我也有点不太同意此篇文章所讲的,mysql的默认隔离级别可能会忧于Read committed

binlog格式使用mixed或是Row

较低的隔离级别 = 较好的性能

21. 注重监控。

任何环境离不开监控,如果少了监控,有可能就会陷入盲人摸象。 推荐zabbix+mpm构建监控。

文章转载地址:http://www.mysqlsupport.cn/21ways-optimize-mysql/