mysql配置之主从复制

主数据库的配置:

[root@bogon mysql]# vi /etc/my.cnf

[mysqld]之后添加如下:

log-slow-queries=mysql-slow.log
log-error=mysql.err
log-bin=mysql-bin
server-id=1

保存退出

[root@bogon mysql]# service mysqld restart

给从数据创建访问权限:

mysql> grant replication slave on *.* to 'rep1'@'192.168.23.132' identified by `123456`;

查看帐号:

mysql> select user,password,host from mysql.user;

查看主服务器状态:

mysql> show master status;

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      126 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

从数据库配置:

检查主数据库帐号是否可用

[root@bogon ~]# mysql -h192.168.23.131 -urep1 -p123456

如果不可用调一下主服务器的防火墙Iptables
[root@bogon ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@bogon ~]# /etc/rc.d/init.d/iptables save

修改从的配置文件:

[root@bogon mysql]# vi /etc/my.cnf

[mysqld]之后添加如下:

log-slow-queries=mysql-slow.log
log-error=mysql.err
log-bin=mysql-bin
server-id=10

保存退出

[root@bogon ~]# service mysqld restart

[root@bogon ~]# mysql -uroot -p123456

mysql> change master to master_host='192.168.23.131', master_user='rep1' , master_password='123456', master_log_file='mysql-bin.000003', master_log_pos=106;

mysql> slave start;

mysql> show slave status \G

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.23.131
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 418
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 563
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...省略很多

这里的Slave_IO_Running与Slave_SQL_Running必须都为YES才行,如果不行,可以使用以下命令查看问题原因:

[root@bogon ~]# tail /var/log/mysqld.log

比如:140220 21:59:29 [ERROR] Error reading packet from server: Misconfigured master - server id was not set ( server_errno=1236)

这句是说主数据库没有设置server-id值,很好理解的。


好了配置完毕,我们测试效果:

主数据库执行以下操作:

mysql> create database first_db;

mysql> use first_db;

mysql> create table first_tb(id int(3),name char(10));

mysql> insert into first_tb values(001,'myself');

从数据库里执行:

mysql> select * from first_db.first_tb;

+------+--------+
| id   | name   |
+------+--------+
|    1 | myself |
+------+--------+
1 row in set (0.00 sec)

好了有结果证明生效了。


(可选内容)在从服务器上设置读锁定有效,确保没有数据库插入或者修改操作
flush tables with read lock;
备份数据库
unlock tables; 解锁

执行完主从复制配置之后,再从主数据库还原即可。


你可能感兴趣的:(mysql配置之主从复制)