mysql的主从(AB)复制

时间:2021-09-30 16:24:52


Mysql AB复制


复制有两种方式:同步复制(msylq-cluster异步复制(mysql-m/s主从备份又称作双机热备)


下面做单向热备实验:
主库master192.168.0.57 从库slave192.168.0.95

1、在masterslave上安装mysql
yum install mysql mysql-server -y (
主从机都要安装mysql)

2、在master上修改/etc/my.cnf文件,添加
server-id=1 //
区别masterslave
log-bin=mysql-bin //
启动二进制日志文件功能
binlog-do-db=test //
二进制文件需要同步的数据库名(不写为同步所有数据)
binlog-ignore-db=mysql //
不同步mysql数据库数据,以免发生因同步了用户信息而从机出现登录问题
然后重启服务:servicemysqld restart

3、授权给用户:
master
登录数据库:
[root@desktop57 ~]# mysql -uroot -pwestos
mysql> grant replication slave,reload,super on *.* to 'slave'@'192.168.0.95'identified by "10086";
mysql> flush privileges;
给主机192.168.0.95用户slave授权,密码10086

在从库slave上测试:[root@desktop95 ~]# mysql -uslave-p10086 -h 192.168.0.57
OK
正常登录,用户建立成功。

4、在slave上修改/etc/my.cnf文件,添加
server-id=2 //
从库ID号,和主库区别开 (注意这一行的注释去掉,不然mysql起不来,可看日志)
master-host=192.168.0.57 //
指定主库IP地址
master-user=slave //
指定授权的用户名
master-password=10086 //
指定授权用户的密码
replicate-do-db=test //
要同步的数据库
replicate-ignore-db=mysql //
忽略mysql数据库的同步

5、在master上查看主库状态:
[root@desktop57 ~]# mysql -uroot -pwestos
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 571 | test | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

可以知道File(mysql-bin.000001),Position(571)

6、在slave上查看从库状态:
[root@desktop95 ~]# mysql -uroot -pwestos

mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master tomaster_host='192.168.0.57',master_user='slave',master_password='10086',master_log_file='mysql-bin.000001',master_log_pos=571;(指定从库应该备份的任务号,以slave用户登录密码10086
Query OK, 0 rows affected (0.13 sec)

mysql> slave start;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
......
Slave_IO_Running: Yes
(从库I/O读写正常)
Slave_SQL_Running: Yes
(从库mysql监听正常)
......
OK
啦,没有的话就检查配置文件是否正确吧~~
我们测试一下看从库slave是否同步了master的数据呢?
[root@desktop57 ~]# mysql -uroot -pwestos
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| grade |
+----------------+
1 row in set (0.00 sec)

mysql> select * from grade;
+-------+-----+-------+--------+
| name | num | grade | notes |
+-------+-----+-------+--------+
| kevin | 14 | 98 | great! |
+-------+-----+-------+--------+
1 rows in set (0.01 sec)

OK,已经同步过来啦!!



本文出自 “我的运维历程” 博客,请务必保留此出处http://nginxs.blog.51cto.com/4676810/1197678