手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL互为主从复制详解----mysql-mm

[日期:2012-02-20] 来源:Linux社区  作者:anljf [字体:大 中 小]
重置日志
MySQL> reset master;
mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 106
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
[root@XKWB5510 ~]# ls -l  /var/mysql/database/data/
total 0

查看data目录下是否有报错文件:
[root@XKWB5510 data]# ls /var/mysql/database/data/

 


重新执行change master to 命令以后,再启动slave,再看一下slave的状态,I/O进程起来了

进程都起来之后,就实施监控
-------------------------------------------------------------------------------------------------------------
报错:
1)
change master导致的:
              Last_IO_Error: error connecting to master 'repl1@A_IP:3306' - retry-time: 60  retries

2)    
在没有解锁的情况下停止slave进程:
     mysql> stop slave;
ERROR 1192 (HY000): Can't execute the given command because you have active locked tables or an active transaction

3)
change master语法错误,落下逗号
mysql> change master to
    -> master_host='211.100.97.250'
    -> master_user='repl2',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=106;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master_user='repl2',
master_password='123456',
master_log_file='mysql-bin.000002' at line 3

4)
在没有停止slave进程的情况下change master
mysql> change master to master_host='211.100.97.246', master_user='repl1', master_password='123456', master_log_file='mysql-bin.000001',master_log_pos=106;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first

5)
A B的server-id相同:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids;
these ids must be different for replication to work (or the --replicate-same-server-id option must be used on
slave but this does not always make sense; please check the manual before using it).
查看server-id
mysql> show variables like 'server_id';
手动修改server-id
mysql> set global server_id=2; #此处的数值和my.cnf里设置的一样就行
mysql> slave start;

6)change master之后,查看slave的状态,发现slave_IO_running 为NO
需要注意的是,做完上述操作之后最后重启mysql进程
---------------------------------------
同步数据情况
A:在A上插入数据
mysql> create table aniya (id int not null auto_increment primary key, str varchar(255) not null);
mysql> insert into aniya(str) values
    -> ('This is a master to master test table');
 
mysql> select * from aniya;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)

查看B的日志:
[root@XKWB5705 var]# ls -lrth XKWB5705-relay-bin.000003
-rw-rw---- 1 mysql mysql 576 Sep 26 12:29 XKWB5705-relay-bin.000003
[root@XKWB5705 var]# more XKWB5705-relay-bin.000003
.in.N

(id int not null auto_increment primary key,
str varchar(255) not null)3
('This is a master to master test table')

-----
 B A主从同步测试


在B上创建表lian,并插入数据
mysql> create table lian (a int,b char(10));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into lian (a,b)values(22,hahah);
ERROR 1054 (42S22): Unknown column 'hahah' in 'field list'
mysql> insert into lian (a,b)values(22,'hahah');
Query OK, 1 row affected (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| lian           |
+----------------+
1 row in set (0.00 sec)

mysql> select * from lian;
+------+-------+
| a    | b     |
+------+-------+
|   22 | hahah |
+------+-------+
1 row in set (0.00 sec)


查看一下B的master日志,证明以上操作成功:
cat mysql-bin.000002                                                                                                                                                                                  
.?Nh?@stdtestcreate table lian (a int,b char(10))??Nl>@stdtestinsert into lian (a,b)values(22,'hahah')

现在查看从服务器A的relay日志,发现日志已经同步了
[root@XKWB5510 var]# cat XKWB5510-relay-bin.000003                                                                                                                                                    
.?Nh?@stdtestcreate table lian (a int,b char(10))??Nl>@stdtestinsert into lian (a,b)values(22,'hahah')

再在从服务器A上看一下数据库是不是存在lian这个表:
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| aniya          |
| lian           |
+----------------+
2 rows in set (0.00 sec)

现在说明数据B A 主 从 同步成功
---------------------------------------------------------------------------
测试A B主从

在A上创建表From246,并插入数据
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| A246           |
| aniya          |
| lian           |
+----------------+
3 rows in set (0.00 sec)

mysql> create table From246(Name varchar(255),Sex varchar(255),Age int(10));
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| A246           |
| From246        |
| aniya          |
| lian           |
+----------------+
4 rows in set (0.00 sec)

mysql> insert into From246 (Name,Sex,Age)values('Zhaoyj','Girl',24);
Query OK, 1 row affected (0.00 sec)

mysql> select * from From246;
+--------+------+------+
| Name   | Sex  | Age  |
+--------+------+------+
| Zhaoyj | Girl |   24 |
+--------+------+------+
1 row in set (0.00 sec)

linux
相关资讯       MySQL数据库教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款