MySQL主从读写分离专题

主机A:192.168.1.101
从机B:192.168.1.102

1、先登录主机 A
mysql>GRANT REPLICATION SLAVE ON *.* TO slave_user@192.168.1.101 IDENTIFIED BY 'admin888';
赋予从机权限,有多台丛机,就执行多次.
2、 打开主机A的my.cnf,输入
server-id               = 1    #主机标示,整数
log_bin                 = /var/log/mysql/mysql-bin.log   #确保此文件可写
read-only              =0  #主机,读写都可以
binlog-do-db         =test   #需要备份数据,多个写多行
binlog-ignore-db    =mysql #不需要备份的数据库,多个写多行
3、打开从机B的my.cnf,输入
server-id               = 2
log_bin                 = /var/log/mysql/mysql-bin.log
(master-host     =192.168.1.101(注意:MySQL5.1以后master-*形式废弃了,采用change master to)
master-user     =backup
master-pass     =123456
master-port     =3306)

mysql> change master to master_host='192.168.1.101',master_user='master_name',ma
ster_password='admin888',master_port=3306,master_log_file='mysql-bin.000001',mas
ter_log_pos=107;

(master_log_file和master_log_pos)的值你可以在服务器上运行 show master status; 来得到。
master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =test #只复制某个库
replicate-ignore-db=mysql #不复制某个库
4、同步数据库
不用太费事,只把主从库都启动即可自动同步,如果不嫌麻烦的话可以把主库的内容导出成SQL,然后在从库中运行一遍
5、先重启主机A的mysql,再重启从机B的mysql
6、验证
在主机A中,mysql>show master status\G;
在从机B中,mysql>show slave status\G;
能看到大致这些内容
File: mysql-bin.000001
Position: 1374
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
可以在主机A中,做一些INSERT, UPDATE, DELETE 操作,看看主机B中,是否已经被修改。

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.1.100
                  Master_User: master_name
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: Y9EMERNXJ2AZ4L2-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: slave_user
          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: 107
              Relay_Log_Space: 107
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1130
                Last_IO_Error: error connecting to master 'master_name@192.168.1
.100:3306' - retry-time: 60  retries: 86400
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
1 row in set (0.00 sec)

以上问题是:connecting... 待解决。

posted @ 2013-06-19 03:30  洒洒  阅读(976)  评论(0编辑  收藏  举报