linux體系下完成mysql熱備份具體步調(mysql主從復制)。本站提示廣大學習愛好者:(linux體系下完成mysql熱備份具體步調(mysql主從復制))文章只能為提供參考,不一定能成為您想要的結果。以下是linux體系下完成mysql熱備份具體步調(mysql主從復制)正文
主從的感化:
1.可以當作一種備份方法
2.用來完成讀寫分別,減緩一個數據庫的壓力
MySQL主從備份道理:
Mysql的主從復制至多是須要兩個Mysql的辦事,固然Mysql的辦事是可以散布在分歧的辦事器上,也能夠在一台辦事器上啟動多個辦事。
假如想設置裝備擺設成為統一台上的話,留意裝置的時刻,選擇兩個分歧的prefix=途徑,同時開啟辦事器的時刻,端口不克不及雷同。
(1)起首確保主從辦事器上的Mysql版原形同(做主從辦事器的准繩是,MYSQL版本要雷同,假如不克不及知足,最最少從辦事器的MYSQL的版本必需高於主辦事器的MYSQL版本 )
(2)在主辦事器上,設置一個從數據庫的賬戶,應用REPLICATION SLAVE付與權限,如:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave001'@'192.168.0.99' IDENTIFIED BY
'123456';
Query OK, 0 rows affected (0.13 sec)
[道理]master 上供給binlog ,
slave 經由過程 I/O線程從 master拿取 binlog,並復制到slave的中繼日記中
slave 經由過程 SQL線程從 slave的中繼日記中讀取binlog ,然後解析到slave中
主從復制年夜條件
須要master與slave同步,由於筆者的數據庫數據量不年夜,所以無需斟酌太多,直接把
master上的data復制到了slave上,然則假如是年夜的數據量,好比像taobao這個的體系
試驗情況預備:
OS: CentOS5.4
Mysql:Mysql-5.0.41.tar.gz
幫助對象:SSH Secure Shell Client
兩台測試IP&辦事器:
Master Server: 192.168.1.2/Linux CentOS5.4/MYSQL 5.0
Slave Server: 192.168.1.3/Linux CentOS5.4/MYSQL 5.0
裝置設置裝備擺設步調:
1、起首在Linux情況下分派好磁盤分區以便留足MySQL數據庫的備份空間
[root@vps mysql]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/simfs 30G 2.0G 29G 7% /
2、MySQL數據庫的裝置:
1>將Mysql-5.0.41.tar.gz經由過程SSH 對象 上傳到Linux體系的home目次下
2>樹立MySQL應用者和群組:
#groupadd mysql
#useradd -g mysql mysql
3>解緊縮Mysql-5.0.41.tar.gz源碼包
#cd /usr/local/sofrware
#tar zxvf Mysql-5.0.41.tar.gz
4>進入源碼目次編譯裝置
#cd /home/Mysql-5.0.41
#./configure --prefix=/usr/local/mysql --with-charset=gbk |注:設置裝備擺設Mysql裝置途徑而且支撐中文
#make |注:編譯
#make install |注:編譯裝置
5>調換/etc/my.cnf文件,進入源碼包,履行敕令
#cd /home/Mysql-5.0.41
#cp support-files/my-medium.cnf /etc/my.cnf
6>完成以上操作今後停止初始化數據庫,進入曾經裝置好的mysql目次
#cd /usr/local/mysql
#bin/mysql_install_db --user=mysql |注:--user=mysql 初始化表而且劃定用mysql用戶
7>設置給mysql和root用戶設定拜訪權限 我們先輩入mysql目次
#cd /usr/local/mysql
#chown -R root /usr/local/mysql 注:設定root能拜訪/usr/local/mysq
#chown -R mysql /usr/local/mysql/var 注:設定mysql用戶能拜訪/usr/local/mysql/var
#chgrp -R mysql /usr/local/mysql 注:設定mysql組可以或許拜訪/usr/local/mysq
8>啟動mysql,進入曾經裝置好的目次
#cd /usr/local/mysql
#bin/mysqld_safe --user=mysql &
9>
修正mysql數據庫超等用戶root的缺省暗碼:
/usr/local/mysql/bin/mysqladmin -u root password 'mysql'
封閉mysql辦事器
cd /usr/local/mysql/bin
./mysqladmin -u root -p shutdown
10>設定開機就啟動mysql,進入源碼目次下
# cd /home/Mysql-5.0.41
# cp support-files/mysql.server /etc/init.d/mysql
# chmod +x /etc/init.d/mysql
# chkconfig --level 345 mysql on
# service mysql restart
Shutting down MySQL. [ 肯定 ]
Starting MySQL [ 肯定 ]
[root@localhost mysql]#
到這裡MySQL就裝好了。
3、設置裝備擺設MySQL5.0的復制(Replication)功效
一.將master設置為只讀。
mysql> flush tables with read lock;
二.用master中的data文件夾調換slave中的data文件夾
好比 用 tar zcvf mysql_data.gz /media/raid10/mysql/3306/data
然後 mv mysql_data.gz /media/raid10/htdocs/blog/wordpress/
由於我的 /media/raid10/htdocs/blog/wordpress/ 是 Nginx 的主目次
所以可以在 slave上,用wget下載這個文件,然後 解壓,並籠罩slave上的data文件
留意:籠罩之前最好備份源文件
三.設置裝備擺設master的my.cnf,添加以下內容
在[mysqld]設置裝備擺設段添加以下字段
server-id=1
log-bin=/media/raid10/mysql/3306/binlog/binlog //這裡寫你的binlog相對途徑名
binlog-do-db=blog //須要同步的數據庫,假如沒有本行,即表現同步一切的數據庫
binlog-ignore-db=mysql //被疏忽的數據庫
這裡給出我的my.cnf設置裝備擺設文件
[client]
character-set-server = utf8
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/webserver/mysql
datadir = /media/raid10/mysql/3306/data
log-error = /media/raid10/mysql/3306/mysql_error.log
pid-file = /media/raid10/mysql/3306/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 16M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
#thread_concurrency = 8
query_cache_size = 20M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 20M
max_heap_table_size = 20M
long_query_time = 3
log-slave-updates
log-bin = /media/raid10/mysql/3306/binlog/binlog
binlog-do-db=blog
binlog-ignore-db=mysql
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 20M
relay-log-index = /media/raid10/mysql/3306/relaylog/relaylog
relay-log-info-file = /media/raid10/mysql/3306/relaylog/relaylog
relay-log = /media/raid10/mysql/3306/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 10M
read_buffer_size = 1M
read_rnd_buffer_size = 6M
bulk_insert_buffer_size = 4M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 20M
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
#master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
#master-host = 192.168.1.2
#master-user = username
#master-password = password
#master-port = 3306
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 20M
innodb_data_file_path = ibdata1:56M: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 = 20M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
#log-slow-queries = /media/raid10/mysql/3306/slow.log
#long_query_time = 10
[mysqldump]
quick
max_allowed_packet = 32M
四.在master機上為slave機添加一同步帳號
mysql> grant replication slave on *.* to 'admin'@'172.29.141.115' identified by '12345678';
mysql> flush privileges ;
五.設置裝備擺設slave的my.cnf,添加以下內容
留意:
1.假如mysql是5.5.3-m3 的版本,只需
在[mysqld]字段下添加以下內容
server-id=2
2.假如是5.0x的版本,須要
在[mysqld]字段下添加以下內容
server-id=2
log-bin=mysql-bin //這是同步的binlog,詳細以你的binlog為准
master-host=172.29.141.112
master-user=admin
master-password=12345678
master-port=3306
master-connect-retry=60 //假如發明主辦事器斷線,從新銜接的時光差;
replicate-do-db=blog //同步的數據庫,不寫本行 表現 同步一切數據庫
replicate-ignore-db=mysql //不須要備份的數據庫
log-slave-update
slave-skip-errors
我的mysql是5.5.3,這裡給出我的slave my.cnf設置裝備擺設文件
[client]
character-set-server = utf8
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-do-db = blog
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/webserver/mysql
datadir = /media/raid10/mysql/3306/data
log-error = /media/raid10/mysql/3306/mysql_error.log
pid-file = /media/raid10/mysql/3306/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 16M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
#thread_concurrency = 8
query_cache_size = 20M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 20M
max_heap_table_size = 20M
long_query_time = 3
log-slave-updates
log-bin = /media/raid10/mysql/3306/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 20M
relay-log-index = /media/raid10/mysql/3306/relaylog/relaylog
relay-log-info-file = /media/raid10/mysql/3306/relaylog/relaylog
relay-log = /media/raid10/mysql/3306/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 10M
read_buffer_size = 1M
read_rnd_buffer_size = 6M
bulk_insert_buffer_size = 4M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 20M
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
#master-connect-retry = 60
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
#master-host=172.29.141.112
#master-user = admin
#master-password = 12345678
#master-port = 3306
server-id = 2
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 20M
innodb_data_file_path = ibdata1:56M: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 = 20M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
#log-slow-queries = /media/raid10/mysql/3306/slow.log
#long_query_time = 10
[mysqldump]
quick
max_allowed_packet = 32M
六.經由過程檢查master的狀況(在master上檢查),為設置裝備擺設slave做預備
mysql> show master status/G;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 13
Current database: blog
*************************** 1. row ***************************
File: binlog.000005
Position: 592
Binlog_Do_DB: blog
Binlog_Ignore_DB: mysql
1 row in set (0.01 sec)
ERROR:
No query specified
從下面的信息,可以看出,master如今應用的binlog是binlog.000005,position是592,那末上面的slave設置裝備擺設必需與這個對應。
其實binlog.000005是以後master應用的binlog日記文件
position是以後master應用的binlog.000005日記文件的地位
簡略懂得為master正在應用哪一個binlog的哪一個數據行(地位)。
七.假如是5.5.3-m3版本mysql,須要啟動slave後,設置裝備擺設與master相干對應的信息(在slave上設置裝備擺設)
留意,這個與第六步絕對應
mysql> stop slave ;
mysql> change master to master_host='172.29.141.112', master_user='admin', master_password='12345678', master_log_file='binlog.000005', master_log_pos=488;
mysql> CHANGE MASTER TO MASTER_CONNECT_RETRY=60;
這個與5.0的設置裝備擺設my.cnf感化是一樣的,設置裝備擺設成與master絕對應的內容
重要是設置裝備擺設slave,讓slave曉得從master的哪一個binlog上的哪一個地位復制數據。所以須要曉得master的ip,user_name,user_passwd,binlog,binlog_position和多長時光銜接一次master
八.開啟slave
mysql> start slave;
九.消除master只讀限制,並做測試
mysql> unlock tables;
mysql> use blog;
mysql> create longxibendi ( a int, b int );
十.從slave上檢查
mysql> use blog;
mysql> show tables;
+-----------------------+
| Tables_in_blog |
+-----------------------+
| longxibendi |
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_terms |
| wp_usermeta |
| wp_users |
+-----------------------+
12 rows in set (0.00 sec)
可以看到勝利了!!
十一.設置裝備擺設進程中,可以用 show slave status/G; 在 slave上
檢查 slave的復制情形
十二.假如湧現甚麼成績,能夠是防火牆的成績
/etc/init.d/iptables stop 封閉 master 上的防火牆,或許停止響應的設置裝備擺設
常碰到的毛病與處理:
1.[mysql]ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
這個毛病,網上有許多說法,其實直接的緣由是mysql辦事器沒有啟動
之前我依照5.0x設置裝備擺設master-slave,然後啟動slave,在銜接slave,就會報這個毛病
後來發明緣由是,mysql slave沒有啟動起來,然後去查毛病日記,發明以下的字段
110505 01:55:20 mysqld_safe mysqld from pid file /media/raid10/mysql/3306/mysql.pid ended
110505 02:04:41 mysqld_safe Starting mysqld daemon with databases from /media/raid10/mysql/3306/data
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use InnoDB's own implementation
110505 2:04:41 InnoDB: highest supported file format is Barracuda.
110505 2:04:41 InnoDB Plugin 1.0.6 started; log sequence number 44338
110505 2:04:41 [ERROR] /usr/local/webserver/mysql/libexec/mysqld: unknown variable 'master-connect-retry=60'
110505 2:04:41 [ERROR] Aborting
110505 2:04:41 InnoDB: Starting shutdown...
110505 2:04:43 InnoDB: Shutdown completed; log sequence number 44348
110505 2:04:43 [Note] /usr/local/webserver/mysql/libexec/mysqld: Shutdown complete
110505 02:04:43 mysqld_safe mysqld from pid file /media/raid10/mysql/3306/mysql.pid ended
110505 02:07:44 mysqld_safe Starting mysqld daemon with databases from /media/raid10/mysql/3306/data
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use InnoDB's own implementation
110505 2:07:44 InnoDB: highest supported file format is Barracuda.
110505 2:07:45 InnoDB Plugin 1.0.6 started; log sequence number 44348
110505 2:07:45 [ERROR] /usr/local/webserver/mysql/libexec/mysqld: unknown variable 'master-host=172.29.141.112'
110505 2:07:45 [ERROR] Aborting
從下面的ERROR,曉得 master-connect-retry=60這個my.cnf中的參數有成績,後來從網上搜材料,發明,mysql5.5.3-m3版本不支撐這個參數,
然後我把這個參數正文失落,又發明不支撐這個參數master-host,從下面的ERROR字段可以看出來。後來,就曉得,5.5.3-m3不克不及按5.0.x那樣設置裝備擺設
本來不須要從my.cnf中設置裝備擺設master相干信息,固然server-id是必需的。其他信息,經由過程 在敕令行中 ,上岸 mysql辦事器設置裝備擺設。
其實server-id的感化是
第一,標識,辨別分歧的slave,第二,避免環備份的產生
2.Last_Error: Last_SQL_Error:等毛病
這個是從 slave上,運轉 show slave status/G; 獲得的。湧現這個成績,最基本的緣由是,slave 沒有與以後的master的binlog 和binlog的position對應上
也就是說,slave傳輸的master binlog 不與以後master正應用的binlog和binlog的行數對應。
3.[ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 86400, Error_code: 2003
這個就是由於防火牆的成績,所以用 /etc/init.d/iptables stop 封閉防火牆就OK了。
4.碰到ERROR 2013 (HY000): Lost connection to MySQL server during query毛病。
在/ect/my.cnf的[mysqld]中添加skip-name-resolve