一 Mysql复制概述:
MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新、删除等等)。因此,要进行复制,必须在主服务器上启用二进制日志。每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存的更新。当一个从服务器连接主服务器时,它通知主服务器,从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,并在本机上执行相同的更新。然后封锁并等待主服务器通知新的更新。从服务器执行备份不会干扰主服务器,在备份过程中主服务器可以继续处理更新。
二 复制实现细节:
MySQL使用3个线程来执行复制功能(其中1个在主服务器上,另两个在从服务器上)。当发出START SLAVE时,从服务器创建一个I/O线程,以连接主服务器并让它发送记录在其二进制日志中的语句。主服务器创建一个线程将二进制日志中的内容发送到从服务器。该线程可以即为主服务器上SHOW PROCESSLIST的输出中的Binlog Dump线程。从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志。第3个线程是SQL线程,由从服务器创建,用于读取中继日志并执行日志中包含的更新。在从服务器上,读取和执行更新语句被分成两个独立的任务。当从服务器启动时,其I/O线程可以很快地从主服务器索取所有二进制日志内容,即使SQL线程执行更新的远远滞后。 三 Mysql主从服务器的配置方法:
确保主从服务器的版本兼容(最好为同一个操作系统和数据库版本)
环境:Master(192.168.1.221)Centos6.0
Hostname=mysql1
Slave(192.168.1.222)Centos6.2 Hostname=mysql2 1 安装Mysql数据库包 mysql-5.1.47-4.el6.x86_64 mysql-libs-5.1.47-4.el6.x86_64 mysql-devel-5.1.47-4.el6.x86_64 mysql-server-5.1.47-4.el6.x86_64 2修改ip和主机名
1) [root@localhost ~]#Vi /etc/sysconfig/network-scripts/ifcf-eth0
DEVICE=\"eth0\"
HWADDR=\"00:0C:29:61:11:E3\" NM_CONTROLLED=\"yes\" ONBOOT=\"yes BOOTPROTO=static IPADDR=192.168.1.221 NETMASK=255.255.255.0 GATEWAY=192.168.1.1
[root@localhost ~]#vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=mysql1
[root@localhost ~]#service network restart
2) [root@localhost ~]#Vi /etc/sysconfig/network-scripts/ifcf-eth0
DEVICE=\"eth0\"
HWADDR=\"00:0C:29:9A:05:35\" NM_CONTROLLED=\"yes\" ONBOOT=\"yes\"
BOOTPROTO=static IPADDR=192.168.1.222 NETMSK=255.255.255.0 GATEWAY=192.168.1.1
[root@localhost ~]#vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=mysql2
[root@localhost ~]#service network restart
3 编辑mysql配置文件,建立主从 1) Master vi /etc/my.cnf [mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock user=mysql
Slave Master # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server-id=1
log-bin=master-bin
log-bin-index=master-bin.index [mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid [root@localhost ~] #service mysqld start
[root@localhost ~]#chkconfig mysqld on
[root@localhost ~] #mysqladmin -u root password \"redhat\" [root@localhost ~]#mysql –u root –p Enter password:
Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 4
Server version: 5.1.47-log Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
mysql>GRANT REPLICATION SLAVE ON *.* TO 'lsh'@'192.168.1.222' IDENTIFIED BY '123456';(此处为实验,密码简单,如线上服务器密码需复杂性) Query OK, 0 rows affected (0.00 sec) 授权成功,建议立即在另一台机器上验证下,如果成功显示mysql>登陆界面便是成功,如下所示:
[root@mysql2 ~]# mysql -u lsh -p -h 192.168.1.221 Enter password:
Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 11
Server version: 5.1.47-log Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement. 如果不可以登录,查看主从服务器的连通性,关闭防火墙,或主帐号的权限是否有问题.
2) Slave
vi /etc/my.cnf
mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0 server-id=2
relay-log-index=slave-relay-bin.index relay-log=slave-relay-bin master-host=192.168.1.221 master-user=lsh
master-password=123456 master-port=3306 [mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid [root@localhost ~] #service mysqld start
[root@localhost ~]#chkconfig mysqld on
[root@localhost ~] #mysqladmin -u root password \"lsh4032\" [root@localhost ~]#mysql –u root –p Enter password:
Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 4
Server version: 5.1.47-log Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
mysql>start slave;(开机从数据库的复制功能)
mysql>show slave status\\G(查看主从服务器的连接状态)
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.221 Master_User: lsh Master_Port: 3306 Connect_Retry: 60
Master_Log_File: master-bin.000003 Read_Master_Log_Pos: 866
Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 252
Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 866 Relay_Log_Space: 407 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec) ERROR:
No query specified
Slave_IO_Running: Yes(网络连接状态) Slave_SQL_Running: Yes(表结构正常) 两部分必须都为YES才可以
四 Error:
1 Slave_SQL_Running: NO(表结构不正常)
Failed to open the relay log './mysqld-relay-bin.000001' (relay_log_pos 4) 报如上错误,就是Slave里面的二进制表没有和Master的二进制表同步。 解决方法:
登入Master数据库:
mysql>show master status;
+-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000003 | 866 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
记住file(master-bin.000003)和Position(866)
mysql> flush tables with read lock;(先锁住主库表)
[root@mysql1 ~]# mysqldump -u root -p --all-database > mysql.sql(导出数据库文件)
[root@mysql1 ~]#scp mysql.sql 192.168.1.222:/root/(把数据库文件传到Slave数据库)
[root@mysql2 mysql]# mysqldump -u root -p --all-database < mysql.sql(导入数据库文件)
登入Slave数据库:
Mysql> stop slave;(停止Slave的复制功能) mysql> reset slave;(重置slave)
Query OK, 0 rows affected (0.00 sec) mysql> change master to
master_host='192.168.1.221',master_user='lsh',master_password='123456',master_log_file='master-bin.000003',master_log_pos=866; Query OK, 0 rows affected (0.05 sec) mysql>start slave;(开启Slave的复制功能)
mysql>show slave status\\G(查看状态,IO和SQL都为YES) 登入Master数据库
mysql>unlock tables;(解除表锁定) mysql>create database etongbao; 登入Master数据库 mysql>show databases;
查看复制情况,如果有表示复制成功。
2 start slave后, Slave_IO_Running和Slave_SQL_Running的状态都为No,并且日志中报类似“Slave I/O thread: Failed reading log event, reconnecting to retry, log 'tc-nsop-test00-bin.000177' position 1019586208”的错误
答:这是由于主库对从库的同步帐号授权不正确造成的,更改并确认授权正确之后,重新start slave,就可以正常同步。
3 如果错误日志中出现如下提示“Warning: You should set server-id to a non-0 value if master_host is set; we force server id to 2, but this MySQL server will not act as a slave.”,并且 Slave_IO_Running和Slave_SQL_Running的状态都为No
答:检查主库的my.cnf,这样的错误是由于没有设置主库的server id或者server id不合法造成的。更改主库server id并重启主库后,start slave,同步可以恢复正常。
4 如果Slave_SQL_Running状态为No,并且错误日志中有类似“Error 'Lock wait timeout exceeded; try restarting transaction' on query. Default database: ……”这样的错误 答:这是由于同步某个行的时候恰好遇到有其他SQL进程对该行进行了锁定,并且锁定时间较长
导致同步进程等待超时。直接start slave即可。 5 同时启动多台从库的同步进程对主库有什么影响
答:通常情况会导致主库的io和网卡流量增加。MySQL的Binlog Dump进程是没有限速的,因此会全速进行binlog读取和数据分发,给主库带来较大的负担。在实际操作中,尽量依次启动从库,并在启动后观察主库io,确保Binlog Dump进程读取完binlog后,再启动下一台。
深灰色:为终端要输入的命令。 黑色:为特别强调特别的注意的。 红色:为需要了解的和掌握的。
Mysql的全量备份
由于在master执行数据库的备份,为了保证数据备份的完整性,需要锁定数据库表,因此备份不适应用master-slave一对一复制的结构,所以选择在slave上执行备份数据库。
[root@mysql2 mysql]#mkdir /backup
[root@mysql2 mysql]#chattr +I –R /backup [root@mysql2 mysql]#vi /tmp/mysqld.sh #! /bin/bash cd /backup
date=`date +\"%y-%m-%d\"` mkdir -p $date/
mysql -u root -predhat -S /var/lib/mysql/mysql.sock -e 'slave stop' #cp -r /var/lib/mysql/mysql/* /backup/$date/data
mysqldump -uroot -predhat --all-database |gzip > /backup/$date/mysql.sql.gz mysql -u root -predhat -S /var/lib/mysql/mysql.sock -e 'start slave’
制定任务计划(每天凌晨1点,执行数据库的全量备份) [root@mysql2 mysql]#crontab –e * */1 * * * sh /tmp/mysqld.sh
[root@mysql2 mysql]#service crond start [root@mysql2 mysql]#chkconfig crond on
因篇幅问题不能全部显示,请点此查看更多更全内容