您的位置:首页 > 博客中心 > 数据库 >

CentOS之MYSQL主从同步配置

时间:2022-03-14 04:39

一、主机master与salver均关闭防火墙iptables,执行service iptables stop命令:


设置SELINUX文件的SELINUX=disabled,如图所示:

二、创建数据库

分别登录master机和slave机的mysql:mysql –u root –p

创建数据库:create database repl;


在/usr/local/mysql目录下建立MYSQL复制的日志配置文件/usr/local/mysql/mysql-bin.log并赋予权限以及改变文件的所有者:

                      [root@master mysql] touch mysql-bin.log

                      [root@master mysql] chmod 755 mysql-bin.log

                      [root@master mysql] chown -R mysql:mysql .

1、修改master机器中mysql配置文件my.cnf,该文件在/etc目录下

在[mysqld]配置段添加如下字段

server-id=1

log-bin=log

binlog-do-db=repl //需要同步的数据库,如果没有本行,即表示同步所有的数据库

binlog-ignore-db=mysql //被忽略的数据库


在master机上为slave机添加一同步帐号
grant replication slave on *.* to identifiedby ‘123456‘;


重启master机的mysql服务:service mysqld restart


用show master status 命令看日志情况

mysql>show master status;

+-----------------+------------+-------------------+-----------------------+

|File               |Position    | Binlog_Do_DB   |Binlog_Ignore_DB |

+-----------------+------------+-------------------+-----------------------+

  log.000003|          98  |repl                |mysql                   |

+-----------------+------------+-------------------+-----------------------+

1 row in set (0.00 sec)



在主机master上添加主机salver的同步账号:

                      mysql> grant replication slave on *.* identified by ‘admin‘;

                      执行如下命令查看:


在[mysqld]配置段添加如下字段

server-id=1

log-bin=log

binlog-do-db=repl //需要同步的数据库,如果没有本行,即表示同步所有的数据库

binlog-ignore-db=mysql //被忽略的数据库

                      在文件中添加如下属性项:

                      log-bin = /usr/local/mysql/mysql-bin.log

                      binlog-ignore-db=mysql

                      binlog-ignore-db=information_schema




在master机上为slave机添加一同步帐号
grant replication slave on *.* to identifiedby ‘123456‘;


重启master机的mysql服务:service mysqld restart


s

select user.host from mysql.user

 可以看见备份账户以及设置完成。下面对数据库表加锁只读。

mysql锁表只读(其他账户登录mysql后无法进行写表操作,防止备份数据库后,主mysql表更新,导致和从数据库内容不一致)


mysql> flush tables with read lock;

                E:查看状态:

                      [root@master mysql] mysql -u root -padmin -e "show master status"

注:File字段的日志名称(mysql-bin.000001)就是从机备份所需要的日志文件。




2、修改slave机中mysql配置文件

解锁数据库:

                      mysql> unlock tables;


同样在[mysqld]字段下添加如下内容

server-id=2


这里开始

master-host=192.168.1.222

master-user=repl

master-password=123456

master-port=3306

master-connect-retry=60

到这里结束,这些参数不能在配置在数据库中 5.1以后的mysql,只能命令

replicate-do-db=repl //同步的数据库,不写本行 表示 同步所有数据库


注意事项(出现错误)
Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数
在从库上执行如下命令;
change master to master_host=‘masterIP‘, master_user=‘slave‘, master_password=‘slvaePASS‘;

change master to master_host=‘10.171.244.109‘,

                                                                       master_user=‘slave‘,

                                                                       master_password=‘admin‘,

                                                                       master_log_file="mysql-bin.000006",

                                                                       master_log_pos=107 ;



然后重启slave机的mysql


在slave机中进入mysql

mysql>start slave;

mysql>show slave statusG;
如果Slave_IO_Running、Slave_SQL_Running状态为Yes则表明设置成功。



四、出现的问题

问题1 当我在执行start slave这条命令时,系统提示

ERROR 1200 (HY000): The server is not configured as slave; fixin config file or with CHANGE MASTER TO,

执行show slave status;又提示Empty set (0.00 sec),想不通问题在哪里

 

或问题2 Slave_SQL_Running: No


后来发现,原来slave已经默认开启,要先关闭再开启

执行 slave stop;

再执行

change master tomaster_host=‘192.168.1.222‘,master_user=‘repl‘,master_password=‘123456‘,master_log_file=‘log.000003‘ ,master_log_pos=98;

然后执行 slave start;

这时 再执行show slave statusG

显示如下:

*************************** 1. row***************************

            Slave_IO_State: Waiting for master to send event

               Master_Host: 192.168.1.222

               Master_User: repl

               Master_Port: 3306

             Connect_Retry: 60

           Master_Log_File: log.000003

       Read_Master_Log_Pos: 98

           Relay_Log_File: mysqld-relay-bin.000002

             Relay_Log_Pos: 229

     Relay_Master_Log_File: log.000003

          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: 229

           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)

显示红色那两行,说明配置成功。

 

 

 

五、测试主从服务器是否能同步

在主服务器上面新建一个表,必须在repl数据下

mysql> use repl

Database changed

mysql> create table test(id int,namechar(10));

Query OK, 0 rows affected (0.00 sec)


mysql> insert into test values(1,‘zaq‘);

Query OK, 1 row affected (0.00 sec)


mysql> insert into test values(1,‘xsw‘);

Query OK, 1 row affected (0.00 sec)


mysql> select * from test;

+------+------+

| id    |name |

+-------+------+

     |zaq   |

     | xsw |

+-------+------+

2 rows in set (0.00 sec)

 


在从服务器查看是否同步过来

mysql> use repl;

Database changed

mysql> select * from test;

+------+------+

| id    |name |

+------+------+

   1 | zaq   |

   1 | xsw |

+------+------+

2 rows in set (0.00 sec)





flush privileges;
然后
show grants for ‘slave‘@‘10.169.6.133‘;

登录数据库遇到
遇到 ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
重启 service mysql restart

错误:Starting MySQL..The server quit without updating PID file ([FAILED]al/mysql/data/iZ23xrasgttZ.pid).
解决方式:
删除 /etc/mysql文件夹
并且修改配置文件中的mstart项 注释掉

注意事项(出现错误)
Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数
在从库上执行如下命令;
change master to master_host=‘masterIP‘, master_user=‘slave‘, master_password=‘slvaePASS‘;

change master to master_host=‘10.171.244.109‘,

                                                                       master_user=‘slave‘,

                                                                       master_password=‘admin‘,

                                                                       master_log_file="mysql-bin.000006",

                                                                       master_log_pos=107 ;
                                                                       
前面参考网址:


http://blog.csdn.net/sxhong/article/details/9212109   (CentOS下设置MySQL的root密码 )

http://blog.csdn.net/limingzhong198/article/details/20578821     (CentOS下MYSQL数据库的主从备份配置 )

后面参考网址:
http://blog.sina.com.cn/s/blog_6eb46a7e0100wmph.html   (centos下MySQL主从同步配置)

http://bbs.csdn.net/topics/380252598     (错误处理)

mysql错误日志一般是mysql安装目录下的data目录下

热门排行

今日推荐

热门手游