mysql 主从复制

基础知识:
需求:
最初只有主数据库,这样即使有数据备份,对业务保证也不够高(业务不大,不需要做主主)。所以新增一台数据库备用服务器,做主从复制。
准备:
在master数据库服务器的操作
添加用于复制的用户
grant replication client,replication slave on *.* to 'angus'@'%.%.%.%' identified by 'password';
修改server  id (只要主从server id不一样就行,可以再从服务器该。)
开启binary logging(一般都开启过)
 
在slaver数据库服务器的操作
修改server id
开启relay-log
开启binary logging(主从切换 做准备)
 
操作:
1,在从服务器上配置好数据库,最后和主数据库是一个版本。
2,sed -i '/^server-id/s#1#2#g' /etc/my.cnf
3,开启relay-log 在/etc/my.cnf 的[mysqld]区域添加下面代码
relay-log=relay-bin
relay-log=relay-bin.index
sed -i '/^server-id/a\relay-log=relay-bin\nrelay-log=relay-bin.index' /etc/my.cnf
4,对主数据进行一次完全备份
[root@localhost ~]# 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.66 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.02 sec)

mysql> quit
Bye
[root@localhost ~]# mysqldump -hlocalhost -uroot -p --all-databases --single-transaction --master-data=2 > /root/all.sql
Enter password:
[root@localhost ~]# ls
all.sql    anaconda-ks.cfg     install.log    install.log.syslog    Music    Pictures    Public    Templates    Videos    yum.sh
[root@localhost ~]# mysql -uroot -p
Enter password:    
Welcome to the MySQL monitor.    Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.66-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@localhost ~]#
把all.sql 传送到从服务器上,并导入到mysql服务器上。
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                                                                     [    OK    ]
Starting mysqld:                                                                                     [    OK    ]
[root@localhost ~]# mysql < all.sql    
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                                                                     [    OK    ]
Starting mysqld:                                                                                     [    OK    ]
[root@localhost ~]# mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.61-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| info.angus.com    |
| know.angus.com    |
| mysql              |
| test               |
+--------------------+
7 rows in set (0.02 sec)
设置从数据库复制数据的主数据(MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=342这些内容可以在less all.sql中找到-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=106;)
mysql> change master to master_host='x.x.x.x',master_user='angus',master_password='password',MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=342;
Query OK, 0 rows affected (0.03 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW SLAVE STATUS\G
如果没有报错,此时主从复制基本上都成功了。
 
主从切换:
由于各种原因,住数据库挂了,并且段时间内无法修复,为了不影响,线上服务,需要及时做主从切换。
[root@localhost ~]# mysql -p
Enter password:    
Welcome to the MySQL monitor.    Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.61-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> STOP SLAVE IO_THREAD;    //确保从服务器已经处理了中继日志中的所有语句
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW PROCESSLIST;         //检查SHOW PROCESSLIST语句的输出,直到你看到Has read all relay log
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User                | Host            | db     | Command | Time | State                                                                                                                                 | Info                         |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
|    5 | system user |                     | NULL | Connect | 2349 | Has read all relay log; waiting for the slave I/O thread to update it | NULL                         |
|    8 | root                | localhost | NULL | Query     |        0 | NULL                                                                                                                                    | SHOW PROCESSLIST |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)

mysql> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> RESET MASTER;
Query OK, 0 rows affected (0.08 sec)

mysql> RESET SLAVE;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                                                                     [    OK    ]
Starting mysqld:                                                                                     [    OK    ]
此时 从服务器升级为主服务器,一般情况下从服务的性能没有主服务器的好,所以当主服务器正常后,首先进入服务找到故障前一刻master状态。
mysql> show master status;
+------------------+----------+--------------+------------------+
| File                         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |            106 |                            |                                    |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)
然后让原来的主服务器做从服务器(开启中继日志),复制数据(首先的有执行复制权限的用户如angus1)
mysql> change master to master_host='x.x.x.x',master_user='angus1',master_password='password',MASTER_LOG_FILE='mysql-bin.000002 , MASTER_LOG_POS=106;    
Query OK, 0 rows affected (0.03 sec)    
等所有数据复制完成后,再做次主从切换。
 
附:
mysql对数据库过滤:有两种形式,一般使用下面一种。
复制过滤器
binlog-do-db = mydb 表示只限制与mydb的修改操作才会记录到二进制日志
binlog-inore-db = mydb

中继日志 过滤
replicate-do-db = mydb
replicate-ignore-db = mydb
replicate-do-table = table
replicate-ignore-table = table
replicate-wild-do-table = db_pattern.tb1_pattern
replicate-wild-ignore-table = db_pattern.tb1_pattern

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章