两台服务器
1. windowsxp (主) mysql5.0.83 ip:192.168.0.155
2. windowsxp (从) ip:192.168.0.167
首先在windows 的CMD中进入mysql命令行模式:
进入命令行模式后输入
GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.0.167' IDENTIFIED BY ''zeasy'' WITH GRANT OPTION;
* 注解
GRANT ALL PRIVILEGES 创建一个拥有所有权限(可以修改 ALL PRVILEGES 改变拥有权限)
ON *.* 它对所有表都可以操作 (可以修改为部分表)
TO 'repl'@'192.168.0.167' (用户和他可以登录的IP地址限制)
IDENTIFIED BY ''123'' (设置账号的初始密码)
WITH GRANT OPTION 允许将自己拥有的权利给第三方使用
设置一个账户可以复制该数据.
修改my.cnf文件
[mysqld]
log-bin = D:/apache/MySQL-5.0.83/mysql-bin.log #日志文件存放地点
server-id = 1 #服务器编号
binlog-do-db =test #需要备份数据库,多个写多行
binlog-ignore-db=mysql #不需要备份的数据库,多个写多行
*如果你的master 同时也是其他计算机的 slave 您必须设置 log-slave-updates
*如果没有设置server-id或设置为0,他将会拒绝所有的slave发送来的请求
*如果您是使用的INNODB存储引擎添加 innodb_flush_log_at_trx_commit=1 和 sync_binlog=1到配置文件您将会获得更高的耐久性和数据一致性
*请确认您的网络设置是否正确,IP地址是否是为绑定的IP,在Debian/Ubuntu的系统中可能比较重要。如果Mater的地址无法被访问,replication将不能建立
在主服务器上执行
mysql> flush tables with read lock; #设置读锁定有效
重启MYSQL服务器:
*这里我出现了一个小小的错误,通过查找错误日志文件发现是因为 mysql-bin.index 文件不存在出错,修改了MYSQL的权限让mysql用户可写就可以了
输入 show mater status;
现在数据库已经停止了更新操作,需要生成主数据库的备份。
可以直接打包压缩,也可以使用 mysqldump 或者ibbackup工具备份。
例: tar -cvf ***.tar data
压缩生成备份文件,防止出错。
解除表锁定:
unlock tables;
进入从数据库~~
首先修改其 my.cnf 中的值
# vi my.cnf
# slave
server-id=2
master-host=192.168.0.155
master-user=admin
master-password=12345678
master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =test #只复制某个库
replicate-ignore-db=mysql #不复制某个库
*如果没有设置server-id或设置为0,他将会拒绝连接Master
*您可以在Slave中打开binlog文件,这样他可以作为一个数据的备份还原,也可以构造更深层次的逻辑!
验证连接主库
# /usr/local/webserver/bin/mysql -h 192.168.0.155-u admin -p 12345678
5、在从库上设置同步
设置连接MASTER MASTER_LOG_FILE为主库的File,MASTER_LOG_POS为主库的
Position
mysql> slave stop;
mysql> change master to master_host='192.168.110.64',master_user='admin', master_password='12345678',
master_log_file='binlog.000003', master_log_pos=1635708;
mysql> slave start;
6、启动从库服务
mysql> slave start;
在主库上建立数据库
mysql> create database yuhongchun;
mysql> create table new (name char(20),phone char(20));
然后在从库上检查是否有yuhongchun数据库,即时可以反映,成功了。
------------------------------------------------------------
一些不成功的测试原因及测试~~~
Master slave 复制错误 Description: Slave_IO_Running:NO Slave_SQL_Running:Yes Seconds_Behind_Master: NULL 本人遇到的Slave_IO_Running:NO的情况有下面两种: 1. 在配置slave同步时因为slave访问master没有权限导致; 2. master上的mysql-bin.xxxxxx文件全被我误删除了; 对于第一种情况,仔细检查数据库访问权限即可解决; 对于第二种情况,下面稍微详细介绍一下: mysql> show slave status/G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.3.21 Master_User: slave Master_Port: 3307 Connect_Retry: 60 Master_Log_File: mysql-bin.000016 Read_Master_Log_Pos: 173 Relay_Log_File: mysqld-relay-bin.000008 Relay_Log_Pos: 98 Relay_Master_Log_File: mysql-bin.000016 Slave_IO_Running: No 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: 173 Relay_Log_Space: 98 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: NULL 1 row in set (0.00 sec) [root@slave mysql]# tail /var/log/mysqld.log 081223 15:51:50 InnoDB: Started; log sequence number 0 43655 081223 15:51:51 [Warning] Neither --relay-log nor --relay-log-index were used; so replication may break when this MySQL server acts as a slave and has his hostname changed!! Please use '--relay- log=/var/run/mysqld/mysqld-relay-bin' to avoid this problem. 081223 15:51:51 [Note] /usr/libexec/mysqld: ready for connections. Version: '5.0.45-log' socket: '/var/lib/mysql/mysql.sock' port: 3307 Source distribution 081223 15:51:51 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000016' at position 173, relay log '/var/run/mysqld/mysqld-relay-bin.000007' position: 98 081223 15:51:51 [Note] Slave I/O thread: connected to master 'slave@192.168.3.21:3307', replication started in log 'mysql-bin.000016' at position 173 081223 15:51:51 [ERROR] Error reading packet from server: Could not find first log file name in binary log index file ( server_errno=1236) 081223 15:51:51 [ERROR] Got fatal error 1236: 'Could not find first log file name in binary log index file' from master when reading data from binary log 081223 15:51:51 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000016', position 173 081223 15:51:58 [Note] Error reading relay log event: slave SQL thread was killed 解决步骤: 重启master库:service mysqld restart mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 98 | | | +------------------+----------+--------------+------------------+ mysql> slave stop; mysql> change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98; mysql> slave start; mysql> show slave status/G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.3.21 Master_User: slave Master_Port: 3307 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 98 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 235 Relay_Master_Log_File: mysql-bin.000001 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: 98 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) |
当两个
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
也就完成了。。。哈哈,成功O(∩_∩)O哈哈~
-------------------------------------------------------------------------------------------------------------------------------------------------
2010、3、10-更新(如何完成数据库主从切换)
保证所有从数据库都已经执行了relay log中的全部更新,在从服务器中执行stop slave io_thread,用show processlist检查,查看状态是否是Has read all relay log,表示更新完成.
1.MySQL> stop slave io_thread;
2.Query OK, 0 rows affected (0.01 sec)
3. 4.MySQL> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info | 7.+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| 5 | system user | | NULL | Connect | -626 | Has read all relay log; waiting for the slave I/O thread to update it | NULL
| | 6 | root | localhost | iea | Query | 0 | NULL | show processlist
| +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
2、在从服务器上执行stop slave,reset master命令,重置成主数据库
1.MySQL>stop slave;
2.Query OK,0 affected (0.00 sec)
3.MySQL>reset master;
4.Query OK,0 affected (0.00 sec)
3、删除新的主服务器数据库目录中的master.info和relay-log.info文件,否则下次重启时还会按照从服务器来启动.
不推荐使用主从切换,可能会丢失数据,不到万不得已,请不要使用该功能!
-------------------------------------------------------------------------------------------------------------------------------------------------
2010、3、21-更新(如何只同步部分表而不是整个数据库)
replicate-wild-do-table=aa.a
replicate-wild-do-table=aa.b //同步表的设置
...
*及时删除没有必要的log文件
1. 在每个从属服务器上,使用SHOW SLAVE STATUS来检查它正在读取哪个日志。
2. 使用SHOW MASTER LOGS获得主服务器上的一系列日志。
3. 在所有的从属服务器中判定最早的日志。这个是目标日志。如果所有的从属服务器是更新的,这是清单上的最后一个日志。
4. 制作您将要删除的所有日志的备份。(这个步骤是自选的,但是建议采用。)
5. 清理所有的日志,但是不包括目标日志。
-------------------------------------------------------------------------------------------------------------------------------------------------
2010、3、21-更新(主主模式)
chown -R mysql:mysql /var/log/mysql
vi /etc/my.cnf //修改Slave配置文件,使之成为主服务器
log-bin=mysql-bin
log-bin=/var/log/mysql/updatelog
binlog-do-db=test
binlog-ignore-db=mysql
mysql
GRANT REPLICATION SLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO 'replication'@'192.168.0.30' IDENTIFIED BY '123456';
//给从服务器用户replication 的同步权限
Grant ALL PRIVILEGES ON test.* TO replication@'%' IDENTIFIED BY '123456';
Flush privileges; //创建用户replication,密码123456,允许所有用户访问数据库test,并刷新权限
vi /etc/my.cnf
master-host=192.168.0.35 //主服务器地址
master-user=replication //指定在主服务器上可以进行同步的用户名
master-password=123456 //密码
master-port=3306 //端口号
master-connect-retry=60 //断点重连时间
replicate-ignore-db=mysql //屏蔽对mysql库的同步,以免有麻烦
replicate-do-db=test //同步数据库名称
mysql
slave stop;
CHANGE MASTER TO
MASTER_HOST='192.168.0.30',
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='updatelog.000001',
MASTER_LOG_POS=98;
//根据主服务器show master的结果,进行从服务器的二进制数据库记录回归,达到同步的效果。
slave start;
show slave status/G;