[摘要]+------------------+----------+--------------+------------------+4、备份主数据库数据 a、备份数据 b、更新bin-log日志 ...
+------------------+----------+--------------+------------------+
4、备份主数据库数据
a、备份数据
b、更新bin-log日志
在这里我们使用mysqldump方式备份数据并使用 -l -F 参数直接在备份数据的时候设置读锁并更新bin-log日志
mysqldump -uroot -p111111 test -l -F > '/tmp/mysql_back/test.sql';
5、将主服务器备份的数据发送到slave服务器
[root@localhost tmp]# scp mysql_back/test.sql 192.168.6.222:/tmp/mysql_back/
6、重置slave服务器上的bin-log日志并在slave服务器中使用备份的数据
mysql> rester master;
[root@localhost tmp]# mysql -uroot -p111111 test -v -f</tmp/mysql_back/test.sql;
7、配置slave服务器中my.cnf参数
a、#配置从服务器server-id =2 (如果有多台从服务器则都有一个唯一的server-id)
server-id = 2
b、#开启bin-log日志
log-bin=mysql-bin
c、#配置需要同步的主机、用户名、密码、端口号
#配置需要同步的主机
master-host = 192.168.6.224
# The username the slave will use for authentication when connecting
# to the master - required
master-user = user2
#
# The password the slave will authenticate with when connecting to
# the master - required
master-password = 123
#
# The port the master is listening on.
# optional - defaults to 3306
master-port = 3306
d、重启mysql让配置文件生效
[root@localhost tmp]# service mysqld restart
如果改方式无法重启mysql服务器可以使用下面的方式
mysql> change master to master_host="192.168.6.224",
master_user="user2",
master_password="123",
master_port=3306,
master_log_file="mysql-bin.000002",master_log_pos=107;
mysql> slave start;
8、查看slave状态
mysql . row master .bin.relaybin.bin.
Master_Log_File:代表主机上用于主备同步的日志文件名,
Read_Master_Log_Pos:代表上一次成功同步到的日志文件中的位置。
如果这两项与先前在主服务器上看到的File及Position的值不相符,则无法正确进行同步。
三、测试
1、在master服务器添加数据并查看bin-log日志状态
mysql> insert into t1 values(13);
Query OK, 1 row affected (0.02 sec)
mysql> insert into t1 values(14);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values(15);
Query OK, 1 row affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
关键词:详细说明mysql学习之主从复制