1、环境1说明 :
system:centos5.5 mysql:5.1.52
master:192.168.0.211 新安装数据库
slave:192.168.0.105 新安装数据库
2,主数据库状态
[root@www opt]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.1.52 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> create databe fmanager; #创建一个新数据库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | fmanager | | infor | | mysql | +--------------------+ 13 rows in set (0.01 sec) #授权一个slave访问的用户 mysql> grant replication slave on *.* to 'slave'@'192.%' identified by 'password'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
2.1 从数据库状态
[root@localhost mysql]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.1.52-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> create databe fmanager; #创建一个新数据库,很重要,必须要与有 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | fmanager | | mysql | +--------------------+ 2 rows in set (0.05 sec)
3、AB同步配置
主数据库服务器端
1、修改配置文件添加及确认有如下行
[mysqld] log-bin=mysql-bin #确保此文件可写 binlog_format=mixed server-id = 1 binlog-do-db=fmanager #需要备份数据(可选) binlog-ignore-db=test,mysql #不需要备份的数据库(可选).
2,修改从数据库配置文件
[mysqld] #log-bin=mysql-bin #slave没有必要开启二进制日志,但是在一些情况下,必须设置,例如,如果slave为其它slave的master,必须设置 relay_log = mysql-relay-bin #配置中继日志 #log_slave_updates = 1 #表示slave将复制事件写进自己的二进制日志,有些人开启了slave的二进制日志,却没有设置log_slave_updates,然后查看slave的数据是否改变,这是一种错误的配置 read_only = 1 #防止从数据库改变数据 server-id=2 #不要使用和主相同的ID master-host =192.168.217.211 #主服务IP master-user=slave #前面主服务器设置的同步用户 master-password=password #前面主服务器设置的同步用户密码 master-port=3306 replicate-do-db=fmanager #只复制某个库(可选) #replicate-wild-ignore-tabes=mysql.% #能同步所有跨数据库的更新 replicate-ignore-db =test,mysql #不复制某个库(可选)
3,重启主从服务器上的mysql服务,查看从数据库,Slave_IO_Running、Slave_SQL_Running是否为YES。是则OK。否则检测配置文件
mysql> show slave status\G; ====== Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: fmanager
4,主服务器创建tables
mysql> use fmanager; Database changed mysql> create table fuser(user int); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +--------------------+ | Tables_in_fmanager | +--------------------+ | fuser | +--------------------+ 9 rows in set (0.01 sec)
5,查看从服务器
mysql> show tables; +--------------------+ | Tables_in_fmanager | +--------------------+ | fuser | +--------------------+ 8 rows in set (0.00 sec)
ok, 数据同步完成
2,如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理
1,主服务器
--主机开两个窗口,一个进入mysql,一个是shell --主机阻断写操作 mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 389 | fmanager | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql>
[root@www ~]# mysqldump -h localhost -u root -p fmanager > fmanager20150407.sql [root@www ~]# scp fmanager20150407.sql [email protected]:~
mysql> unlock tables;
2,从服务器
mysql> create database fmanager; mysql> exit [root@localhost var]# mysql -h localhost -u root -p fmanager < /root/fmanager20150407.sql Enter password:
mysql> slave stop; Query OK, 0 rows affected (0.00 sec) mysql> reset slave; Query OK, 0 rows affected (0.02 sec) mysql> change master to master_host='192.168.0.211',master_user='slave',master_password='password',master_log_file='mysql-bin.000001',master_log_pos=389; Query OK, 0 rows affected (0.02 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G; Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.211 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 389 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: fmanager =====
ok, 更新主服务器上的数据,就可以看到从服务器上也更新了