ITPub博客

首页 > 数据库 > MySQL > Freebsd 8.3 下Mysql 主从配置

Freebsd 8.3 下Mysql 主从配置

原创 MySQL 作者:shawnloong 时间:2015-06-25 21:49:56 0 删除 编辑
Freebsd 8.3 下Mysql 主从配置
复制介绍:摘自 MYSQL 参考手册
MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。(这与同步复制可以进行对比,同步复制是MySQL簇的一个特征—参见第17章:MySQL簇)。主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。

如果你想要设置链式复制服务器,从服务器本身也可以充当主服务器。

请注意当你进行复制时,所有对复制中的表的更新必须在主服务器上进行。否则,你必须要小心,以避免用户对主服务器上的表进行的更新与对从服务器上的表所进行的更新之间的冲突。

单向复制有利于健壮性、速度和系统管理:

·         主服务器/从服务器设置增加了健壮性。主服务器出现问题时,你可以切换到从服务器作为备份。

·         通过在主服务器和从服务器之间切分处理客户查询的负荷,可以得到更好的客户响应时间。SELECT查询可以发送到从服务器以降低主服务器的查询处理负荷。但修改数据的语句仍然应发送到主服务器,以便主服务器和从服务器保持同步。如果非更新查询为主,该负载均衡策略很有效,但一般是更新查询。

·     使用复制的另一个好处是可以使用一个从服务器执行备份,而不会干扰主服务器。在备份过程中主服务器可以继续处理更新

1.环境
两台装好的MYSQL 版本最好一致
mysql> select version(),current_date();
+------------+----------------+
| version()  | current_date() |
+------------+----------------+
| 5.5.32-log | 2013-07-18     |
+------------+----------------+
1 row in set (0.00 sec)

OS:
mail# uname -a
FreeBSD mail.test.com 8.3-RELEASE FreeBSD 8.3-RELEASE #0: Mon Apr  9 21:23:18 UTC 2012     root@mason.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  amd64

主:192.168.20.199 从:192.168.20.222

2. 配置主服务器
修改配置文件
ee /usr/local/etc/my.cnf
server-id       = 1 #主数据库ID一般默认即可
log-bin=mysql-bin #log进程
binlog_format=mixed  #binlog模式
三种模式来实现:基于SQL语句的复制(statement-based replication, SBR),基于行的复制(row-based replication, RBR),混合模式复制(mixed-based replication, MBR)
如果binlog采用了 MIXED 模式,那么在以下几种情况下会自动将binlog的模式由 SBR 模式改成 RBR 模式。
当DML语句更新一个NDB表时
当函数中包含 UUID() 时
2个及以上包含 AUTO_INCREMENT 字段的表被更新时
行任何 INSERT DELAYED 语句时
用 UDF 时
expire_logs_days=5 #log过期时间建议设置一个
binlog-ignore-db = mysql #忽略的db,这里可以写多行
binlog-ignore-db = information_schema
replicate-do-db = extmail #复制的数据库

修改完后重启mysql
/usr/local/etc/rc.d/mysql-server restart

授权REPLICATION SLAVE
登录主库
mysql -u root -p
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.20.222' IDENTIFIED BY 'Abc123Q';
FLUSH TABLES WITH READ LOCK;
退出重新备份数据库,将备份数据库拷贝到从库
mail# mysqldump -u root -p --all-databases --lock-tables=false > all12.sql
scp -P 22 all12.sql root@192.168.20.222:/home/root
查看主库状态

mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000001 |      877 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
解锁数据表
mysql>UNLOCK TABLES;

3.从服务器配置
mail# ee /usr/local/etc/my.cnf
修改以下参数
server-id       = 2 
#注此id不要和主库一样 否则会报"these ids must be different for replication to work"此类的错误
log-bin=mysql-bin
binlog_format=mixed
expire_logs_days=5
 
导入数据
mysql -u root -p

重启mysql
/usr/local/etc/rc.d/mysql-server restart

进入mysql配置mysql slave
mysql -u root -p

change master to master_host='192.168.20.199', master_port=3306, master_user='slave’, master_password='Abc123Q’, master_log_file=’mysql-bin.000001', master_log_pos=373;
#此位置master_log_pos和主库一致
启动 slave进程。
mysql>start slave;

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.20.199
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 877
               Relay_Log_File: mail-relay-bin.000002
                Relay_Log_Pos: 757
        Relay_Master_Log_File: mysql-bin.000001
             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: 877
              Relay_Log_Space: 912
              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
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR:
No query specified
检查是否error,如果有error先停止slave stop slave,然后修改完后start slave




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24486203/viewspace-1712428/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-08-29

  • 博文量
    110
  • 访问量
    292637