当前位置:首页 > 计算机相关 > 建站之路 > 正文内容

配置MYSQL主从数据同步

piikee12年前 (2012-11-16)建站之路706
主机配置my.ini
server-id=10
log-bin=mysql-bin
log="D:/SOFT_PHP_PACKAGE/mysql/mylog.log"
binlog-do-db = piikeesoft
从机配置my.ini
datadir="D:/SOFT_PHP_PACKAGE/mysql/Data/"
server-id=11
log-bin=mysql-bin
log="D:/SOFT_PHP_PACKAGE/mysql/mylog.log"
replicate-do-db = piikeesoft #这里是要同步的数据库
master-host=113.10.172.***  #主服务器地址
master-port=3306
master-user=用户名
master-password=密码
master-connect-retry=60
log_slave_updates=1
同步多个数据库重复设置选项binlog-do-db=数据库名   和replicate-do-db=数据库名
每次停止数据库后最好删除报错文件,包括master.info文件。。。。
其他参考:
MySQL-master1:192.168.72.128
MySQL-master2:192.168.72.129
OS版本:CentOS 5.4
MySQL版本:5.5.9(主从复制的master和slave服务器中的mysql版本相同或者master版本高于slave版本)
一、MySQL master-master配置
1、修改MySQL配置文件
两台MySQL均如要开启binlog日志功能,开启方法:在MySQL-master1  配置文件/etc/my.cnf
[MySQLd]段中加上
user = mysql
log-bin=mysql-bin
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
在MySQL-master2  配置文件/etc/my.cnf
[MySQLd]段中加上
user = mysql
log-bin=mysql-bin
server-id= 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
将两个配置文件保存,分别重启mysql服务器
server_id值必须为2到232-1之间的一个正整数值。ID值唯一的标识了复制群集中的主从服务器,因此它们必须各不相同。
binlog-do-db=database 是要记录日志的数据库;
同步多个数据库重复设置选项binlog-do-db=test   和replicate-do-db=test
例如
binlog-do-db=test1
replicate-do-db=test1
binlog-do-db=test2
replicate-do-db=test2
binlog-ignore-db 是不要记录日志的数据库名,多个数据库中间用逗号(,)隔开;
mysql配置文件my.cnf中    log-slave-updates表示 如果一个MASTER 挂掉的话,另外一个马上接管。
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1   指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
auto_increment_offset = 1
auto_increment_increment = 2
这样A的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID了
auto_increment_offset = 2
auto_increment_increment = 2
这样B的auto_increment字段产生的数值是:2, 4, 6, 8, …等偶数ID了
你的auto_increment字段在不同的服务器之间绝对不会重复,所以Master-Master结构就没有任何问题了。当然,你还可以使用3 台,4台,或者N台服务器,只要保证auto_increment_increment = N 再设置一下auto_increment_offset为适当的初始值就可以了,那样,我们的MySQL可以同时有几十台主服务器,而不会出现自增长ID 重复。
2、将192.168.72.128设为192.168.72.129的主服务器
在192.168.72.128上新建授权用户
MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
Query OK, 0 rows affected (0.00 sec)
MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 |      374 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
------------------------------------------------------------------------------
在192.168.72.129将192.168.72.128为自己的主服务器
MySQL> change master to master_host='192.168.72.128',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
MySQL> start slave;
Query OK, 0 rows affected (0.00 sec)
MySQL> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.72.128
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL-bin.000003
Read_Master_Log_Pos: 374
Relay_Log_File: MySQL-master2-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: MySQL-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: 374
Relay_Log_Space: 235
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
1 row in set (0.00 sec)
红色标注部分为192.168.72.128主机中show master status命令结果中的file postion两个值
3、将192.168.72.129设为192.168.72.128的主服务器
在192.168.72.129上新建授权用户
MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
Query OK, 0 rows affected (0.00 sec)
MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 |      374 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
----------------------------------------------------------------------------------
在192.168.72.128将192.168.72.129为自己的主服务器
MySQL> change master to master_host='192.168.72.129',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
MySQL> start slave;
Query OK, 0 rows affected (0.00 sec)
MySQL> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.72.129
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL-bin.000003
Read_Master_Log_Pos: 374
Relay_Log_File: MySQL-master2-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: MySQL-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: 374
Relay_Log_Space: 235
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
1 row in set (0.00 sec)
如果主数据库有数据的话
数据库锁表操作,不让数据再进行写入动作。mysql> FLUSH TABLES WITH READ LOCK ;
用命令mysqldump
备份数据库。
在master服务器执行
shell> mysqldump -uroot -p123456 --master-data  --opt test1 > backup-file.sql
看主数据库的状态mysql> show master status;
记录File 和Position 项的值
注:由于没有锁定主服务器,这里记录的主服务器二进制日志position值可能会大于做mysqldump时的值,这将导致从服务器丢失在此期间的更新。如果可以保证在此期间主服务器不会出现创建新表的更新,那么丢失的影响不大;否则,将导致从服务器复制线程失败,这时必须在做mysqldump时锁定主服务器。
-----------------------------------------------
从服务器中my.cnf文件中加入选项
skip-slave-start
skip-slave-start 表示从mysql服务器启动时不启动同步线程,这就要在启动从服务器之后,手工启动同步线程,在mysql> 提示符下面运行“start slave”就可以
保存my.cnf后
执行
shell> mysqladmin -uroot -p123456 create test1
shell> mysql -uroot -p123456 test1 < backup-file.sql
启动从服务器线程
mysql>start slave;
取消主数据库锁定mysql>UNLOCK TABLES;
4、其他命令
1、查看复制进度
需要在主库上运行
mysql>show processlist \G;
2、主服务器上的相关命令:
show processlist;
show master status
show slave hosts
show {master|binary} logs
show binlog events
purge {master|binary} logs to 'log_name'
purge {master|binary} logs before 'date'
reset master(老版本flush master)
set sql_log_bin={0|1}
3、从服务器上的相关命令:
slave start
slave stop
slave stop IO_THREAD //此线程把master段的日志写到本地
slave start IO_THREAD
slave stop SQL_THREAD //此线程把写到本地的日志应用于数据库
slave start SQL_THREAD
reset slave
set global sql_slave_skip_counter
load data from master
show slave status(SUPER,REPLICATION CLIENT)
CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= //动态改变master信息
PURGE MASTER [before 'date'] 删除master端已同步过的日志
=========================================================================
skip-slave-start
server-id = 1
log-bin=C:/Program Files/MySQL/MySQL Server 5.0/00/repbinlog
log-error=C:/Program Files/MySQL/MySQL Server 5.0/rep/rep.err
relay-log=C:/Program Files/MySQL/MySQL Server 5.0/rep/beltal_relay_log
sync_binlog=1
log-slave-updates
innodb_flush_log_at_trx_commit=1
binlog-do-db=sync
#slave
master-host     =   192.168.1.144
master-user     =   replicate
master-password =   replicate
master-port     =  3306
master-connect-retry=60
replicate-do-db = sync
report-host=192.168.1.80

扫描二维码推送至手机访问。

版权声明:本文由萍客小居[www.piikee.net]发布,如需转载请注明出处。

本文链接:https://www.piikee.net/980.html

分享给朋友:

相关文章

网站SEO排名的一些因素

站长们每天都希望网站有好的流量,获得好的排名,那么大家有没有注意影响网站排名的因素有哪些呢?首先,看看网站的内部有利因素1. URL中出现关键词2. 网页Title中出现关键词3. 常规内容中出现关键词4. 在页面的第一段中出现关键词5....

IIS7.5只能上传2M,discuz论坛附件上传失败的解决办法

问题:IS7.5只能上传2M的数据,discuz论坛附件上传显示100%,然后显示上传失败,后台找不到附件。网上一般解决方案:IIS修改方案:1. 修改IIS的applicationhost.config打开 %windir%\system...

一段邪恶的MYSQL数据库修复命令。。。

好吧,MYSQL坏了就用他。。。虽然哥的MYSQL还没坏过。。首先,服务器开始菜单->运行>输入cmd->出现了个东东。。。在这个东东里面呢,不断输入“cd..”直到出现C:\>,然后邪恶的事情开始了。输入你的MYS...

解决PHP-CGI爆满问题

解决PHP-CGI爆满问题在C:\WINDOWS\system32\inetsrv\fcgiext.ini 文件中可以设定php-cgi进程相关参数,如:打开这个文件, 并用查找的方式 来查找maxInstances=把这个值改小建议 51...

解决PHP被DDOS问题

在C盘的 PHP目录下面有个 php.ini 文件,打开它用查找   allow_url_fopen   找到相对应的, 然后把 On 改成 Off找到extension=php_sockets.dll改成;extension=php_so...

ISAPI_Rewrite3出现404错误

很重要一个原因就是.htaccess文件没有给权限权限,要记住记住了!!!...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。