mysql主从配置的另一种方法

当前系统情况,有三台数据库服务器,10.100.4.224,10.100.4.226,10.100.4.227

准备用10.100.4.224做主库,10.100.4.22610.100.4.227 做从库

需要执行下面的操作

1. 创建主从同步用 户

224,226和 227上创建同步账户

在224上执行:

mysql>grant all privileges on *.* to 'repl'@'10.100.4.226' identified by 'repl' with grant option;

mysql>grant all privileges on *.* to 'repl'@'10.100.4.227' identified by 'repl' with grant option;

在226上执行:

mysql>grant all privileges on *.* to 'repl'@'10.100.4.224' identified by 'repl' with grant option;

在227上执行:

mysql>grant all privileges on *.* to 'repl'@'10.100.4.224' identified by 'repl' with grant option;

2. 修改配置文件my.cnf

修改3台机器的my.cnf文件

主库224

打开下面的选项

log-bin=mysql-bin

innodb_flush_log_at_trx_commit = 1

打开并设置

server-id=1

注意server-id选项必须打开,并且集群中每个mysql都要有一个单独的值,不能重复。一般设置主为1,从为2, 3, 4....。手册上说只要不相同什么整数都没有关系,可以设置为IP的值,如:226227

同步数据库设置,添加下面的内容,只设置不同步的库

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

replicate-ignore-db=mysql

replicate-ignore-db=infomation_schema

,如果要只设置同步的库

用下面的两个参数:binlog-do-db,replicate-do-db

在从库226

[mysqld]下面打开下面的选项

log-bin=mysql-bin

innodb_flush_log_at_trx_commit = 1

打开并设置

server-id=2

添加下面的设置

sync-binlog=1

#从库设置

master-host=10.100.4.224

master-user=repl

master-password=repl

master-port=3306

master-connect-retry=10

#不备份的数据库

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

replicate-ignore-db=mysql

replicate-ignore-db=infomation_schema

#忽略错误

slave-skip-errors=all

227226的配置相同,唯一的区别是

server-id=3

3. 备份主库数据,恢复到从库

为了避免风险,新主库mysql现在最好不要对外提供服务,就是说主mysql的已有数据不要在动了。 执行下面锁表的命令:
 mysql> flush tables with read lock;

备份主库数据,假设我们只有个数据库LearnJSP

#mysqldump --opt LearnJSP >LearnJSP.sql

把备份的数据文件传到226227上并恢复

恢复的命令是:

# mysqladmin create LearnJSP

# mysql LearnJSP<LearnJSP.sql

4. 启动主从

重启三台数据库,启动顺序是227,226,224

先从224起依次关闭数据库,命令是:

# mysqladmin -uroot shutdown

从227开始启动数据库,命令是:

# mysqld_safe --user=root &

5. 检查状态

主库224上先执行

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |

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

| mysql-bin.000001 |      257 |              | mysql,information_schema | 

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

1 row in set (0.00 sec)

查看从库的状态是否一致

226227上执行

mysql> show slave status\G;

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

             Slave_IO_State: Waiting for master to send event

                Master_Host: 10.100.4.224

                Master_User: repl

                Master_Port: 3306

              Connect_Retry: 10

            Master_Log_File: mysql-bin.000001

        Read_Master_Log_Pos: 257

             Relay_Log_File: idsapp2-relay-bin.000002

              Relay_Log_Pos: 394

      Relay_Master_Log_File: mysql-bin.000001

           Slave_IO_Running: Yes

          Slave_SQL_Running: Yes

            Replicate_Do_DB: 

        Replicate_Ignore_DB: mysql,infomation_schema

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

            Relay_Log_Space: 394

            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)

察 看Slave_IO_State,一般应该 为"Waiting for master to send event"

此时Slave_IO_RunningSlave_SQL_Running应该为yes,如果不是,在slave端执行: stop slave; start slave; 重 启复制进程。

Replicate_Do_DB为 要复制的数据库,和my.cnf配置的相同。

master端执行:

mysql> show master status; 

对 比File, Position的值和上面slave端的Master_Log_File,Read_Master_Log_Pos,两边相同则表示搭建完成。

6. 测试

在主库224上执行建表,插入,删除,修改等操作,看看slave是否同步了。

停掉salve,然后执行上面的操作,然后启动从库,看看是否同步了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值