mysql主从备份
记录于:2011年6月3日11时22分
mysql服务器配置
Section titled “mysql服务器配置”port=3306#default-character-set=utf8loose-default-character-set=utf8 (用于mysql-bin在使用时无法使用此选项)port=3306skip-lockingkey_buffer = 256Mmax_allowed_packet = 1Mtable_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size = 16Mdefault-character-set=utf8init_connect='SET NAMES utf8'max_connections=1000ft_min_word_len=1thread_concurrency = 8log-bin=mysql-binexpire_logs_days=10datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysqlserver-id = 1* Default to using old password format for compatibility with mysql 3.x* clients (those using the mysqlclient10 compatibility package).old_passwords=1
* Disabling symbolic-links is recommended to prevent assorted security risks;* to do so, uncomment this line:* symbolic-links=0
log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidquickmax_allowed_packet=16Mno-auto-rehashkey_buffer=128Msort_buffer_size=128Mread_buffer=2Mwrite_buffer = 2Mkey_buffer=128Msort_buffer_size=128Mread_buffer=2Mwrite_buffer = 2Minteractive-timeout
注: 所有主从服务器的servier-id不能相同
- 假设主服务器ip: 192.168.1.94
- 从服务器ip: 192.168.1.92
在主服务器上进行的操作
Section titled “在主服务器上进行的操作”-
授权
GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.1.92' identified by 'mysql';flush privileges; -
查询主数据库状态
mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000004 | 98 | | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)记下file及position的值,在后面操作会用到
-
配置从服务器
-
修改从服务器的my.cnf文件中server-id值, 保证唯一性
-
启动服务
service mysqld start或/usr/local/mysql/bin/mysqld_safe --user=mysql & -
登录mysql
mysql -uroot -p -
执行同步sql语句
mysql>change master tomaster_host='192.168.1.94',master_user='user',master_password='mysql',master_log_file='mysql-bin.000004',master_log_pos=98; -
主从同步检查
mysql> show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.1.94Master_User: userMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000004Read_Master_Log_Pos: 98Relay_Log_File: mysqld-relay-bin.000009Relay_Log_Pos: 235Relay_Master_Log_File: mysql-bin.000004Slave_IO_Running: `Yes`Slave_SQL_Running: `Yes`Replicate_Do_DB:Replicate_Ignore_DB:..........slave_io及slave_sql进程都必须正常运行, 状态为Yes;否则都是不正确的状态
-
如果主数据库服务器已经存在用户数据,那么在进行主从复制前,需要以下处理
- 主数据库锁表操作
mysql> flush tables with read lock;
- 查看主数据库的状态
mysql> show master status;
记下file及position的值
- 把主服务器的数据文件复制到从服务器
- 取消主数据库锁定
mysql> unlock tables;
- 从服务器的操作和前面的步骤一样(略)
常见主从同步问题
Section titled “常见主从同步问题”slave:mysql> stop slave; #停掉slave的复制先。master:mysql> flush tables with read lock; #锁掉master服务器的所有表,禁止写入。master:mysql> show master status; #还是上面的语句,查看并记录下 File mysql-bin.000002, Position 1087+------------------+----------+----------------------------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+----------------------------------+------------------+| mysql-bin.000002 | 1087 | test_db | |+------------------+----------+----------------------------------+------------------+1 row in set (0.00 sec)chluo@master:~$ mysqldump test_db > test_db.sql #在命令行中导出DB的数据,这里是bash操作:)master:mysql> unlock tables; #导出完成之后,解锁。 master可以继续跑起来了。chluo@slave:~$ mysql test_db < test_db.sql #在slave的命令行中导入DB的数据,这里又是bash操作:)slave:mysql> change master to -> master_log_file='mysql-bin.000002', #将这里修改为刚记录下来的数据 -> master_log_pos=1087; #还有这里slave:mysql> start slave;slave:mysql> change master to master_host='xx.xx.79.54', master_user='slave', master_password='xxxxx',master_log_file='binlog.000001',master_log_pos=8843;change master to master_host='xx.xx.92.32', master_user='slave', master_password='xxxxx', master_log_file='binlog.000011', master_log_pos=120611482;init-file=/usr/local/mysql/init.sqlUSE adserv;INSERT INTO adserv.adindex (SELECT * FROM adservp.adindex);INSERT INTO adserv.adindex_goto (SELECT * FROM adservp.adindex_goto);slave:mysql> set session transaction isolation level read committed;show variables like "%transaction%";show variables like "%binlog%";
/usr/local/mysql/bin/mysqlbinlog /opt/projects/mysql/data/binlog.000149 --start-datetime='2011-6-16 17:13:50' |grep '121212'| lessslave stop;set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;slave start;