码迷,mamicode.com
首页 > 数据库 > 详细

mysql主从复制环境搭建

时间:2015-05-30 01:55:43      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:mysql   主从复制、   

准备安装包
从ORACLE官方网站下载最新版本的MYSQL客户端和服务器安装包,MYSQL目前采用整体打包下载,没单独的安装包下载,直接下载

V45446-01整体压缩包,里面包含最新版本MYSQL5.6.19的客户端和服务器安装包。

MySQL-client-advanced-5.6.19-1.el6.x86_64.rpm(V45446-01)
MySQL-server-advanced-5.6.19-1.el6.x86_64.rpm(V45446-01)


在redhat安装,还要准备MYSQL 关依赖包:
perl-5.10.1-136.el6.x86_64.rpm
libaio-0.3.107-10.el6.x86_64.rpm
mysql-lib5-5.1.71-1.el6.x86_64.rpm


一、mysql安装


1、安装系统、配置ip、配置/etc/hosts 文件
/etc/hosts 文件
192.168.1.21 replm
192.168.1.22 replsl


配置ip
[root@replm mysql]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=08:00:27:a7:0e:e2
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.1.21
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
IPV6INIT=no
USERCTL=no



2、上传rpm 安装包
3、关闭防火墙 (需要关闭,否则同步可能不成功,我遇到此问题,后面详细介绍遇到的问题)
service iptables stop     --停止防火墙
chkconfig iptables off   --配置系统重启不启动防火墙


4、配置yum源


mount  /dev/cdrom  /mnt/    --挂载光驱


 vi /etc/yum.repos.d/mysql.repo


[mysql]
name=mysql
baseurl=file:///mnt
gpgcheck=0
enable=1


5、删除系统自带的 mysql-libs* 包,否则安装MySQL-server-advanced-5.6.19-1.el6.x86_64.rpm 报错,删除命令为:
yum -y remove mysql-libs-*



6、安装rpm包,命令为 rpm
rpm -ivh MySQL-devel-advanced-5.6.19-1.el6.x86_64.rpm
rpm -ivh MySQL-client-advanced-5.6.19-1.el6.x86_64.rpm
rpm -ivh MySQL-server-advanced-5.6.19-1.el6.x86_64.rpm



7、启动mysql


service mysql start
设置开启自启动
chkconfig mysql on



8、登陆MYSQL,并设置root用户密码


Mysql安装后,会将默认密码放在/root/.mysql_secret文件中,第一次登陆时,请采用改密码登陆:
cat /root/.mysql_secret
# The random password set for the root user at Thu Jul 10 16:23:52 2014 (local time): GeEUu0kRfQKatLY0


使用默认密码登陆mysql:
mysql -uroot -p GeEUu0kRfQKatLY0

设置新的root密码,假设设置为111111

mysql> SET PASSWORD = PASSWORD(‘111111‘);

设置root用户登陆权限,使用户可以使用客户端远程登陆:


mysql> use mysql;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "111111";
mysql> update user set password = password(‘111111‘) where user=‘root‘;

刷新权限,权限修改才能生效:
mysql>flush privileges;


注意:远程登陆并验证root用户访问没问题,再开始配置主从复制。


二、 MYSQL主从配置


1、先关闭服务:
 service mysql stop


修改mysql配置文件,主从复制要求台机器server-id 不能相同,从库必须打开relay-log:

1)修改master 配置文件
 vi /etc/my.cnf
server-id = 111
log-bin=/var/lib/mysql/binlog/mysql-bin.log     
relay-log=/var/lib/mysql/relaylog/mysql-relay.log


创建日志路径:
mkdir /var/lib/mysql/binlog
mkdir /var/lib/mysql/relaylog


修改路径权限:
chown mysql:mysql /var/lib/mysql/binlog -R
chown mysql:mysql /var/lib/mysql/relaylog -R


2)修改从slave 配置文件
vi /etc/my.cnf
加入以下内容:
server-id = 222
log-bin=/var/lib/mysql/binlog/mysql-bin.log
relay-log=/var/lib/mysql/relaylog/mysql-relay.log


创建日志路径:
mkdir /var/lib/mysql/binlog
mkdir /var/lib/mysql/relaylog


修改路径权限:
chown mysql:mysql /var/lib/mysql/binlog -R
chown mysql:mysql /var/lib/mysql/relaylog -R


2、master 和salve 启动mysql实例

service mysql start


3、登陆master 和slave 设置专用的复制账号repl,主从都执行

设置主从账号,起名为repl,设置其访问范围为192.168.1网段内,密码123456:


mysql > grant replication slave,replication client on *.* to ‘repl‘@‘192.168.1.%‘ identified by ‘111111‘;
刷新权限:
mysql > flush privileges;


查看主库当前位置(主库执行)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)



3、从库开始同步,同步位置为主库当前位置(从库执行):
mysql>change master to master_host=‘192.168.100.6‘,master_user=‘repl‘,
master_password=‘123456‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=235;

启动slave
mysql> start slave;

检查从库是否复制正常(Slave_IO_Running和Slave_SQL_Running都Yes,必须都为yes ,其他状态都不复制)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.21
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 120
               Relay_Log_File: replsl-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.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: 120
              Relay_Log_Space: 457
              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: 111
                  Master_UUID: 7b4e0e11-05dc-11e5-b3be-080027a70ee2
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)


配置完成


三、测试

1、master端上创建数据库和表、并插入数据

mysql> create database zxmdb;
Query OK, 1 row affected (0.00 sec)

mysql> use zxmdb;
Database changed
mysql> create table t1 (id int,name char(10));
Query OK, 0 rows affected (0.09 sec)

mysql> insert into t1 values (1,‘zxm‘);
Query OK, 1 row affected (0.02 sec)

mysql> insert into t1 values (1,‘zxm‘);
Query OK, 1 row affected (0.02 sec)

mysql> insert into t1 values (1,‘zxm‘);
Query OK, 1 row affected (0.05 sec)

mysql> insert into t1 values (2,‘king ‘);
Query OK, 1 row affected (0.04 sec)


2、slave端验证

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| zxmdb              |
+--------------------+
5 rows in set (0.00 sec)

mysql> use zxmdb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from t1;
+------+------+
| id   | name |
+------+------+
|    1 | zxm  |
|    1 | zxm  |
|    1 | zxm  |
|    2 | king |
+------+------+
4 rows in set (0.00 sec)


四、总结

遇到问题
1、安装过程中报错,无法安装 MySQL-server-advanced-5.6.19-1.el6.x86_64.rpm ,原因是redhat 自带安装了 mysql-libs-

5.1.66-2.el6_3.x86_64,已经存在某些文件,导致无法安装。
解决方法: 删除 mysql-libs-5.1.66-2.el6_3.x86_64
yum -y remove mysql-libs-*
然后安装成功

2、slave端执行show slave status 输出     Slave_IO_Running: Connecting ,导致不能复制数据;
 Last_IO_Error: error connecting to master ‘repl@192.168.1.21:3306‘ - retry-time: 60  retries: 36
分析原因:slave端无法连接到master端
1)repl 用户权限不对
2)端口被占用
3)防火墙阻挡
 
1)测试连接,果然报错
 mysql -u repl -p111111 -h 192.168.1.21
Warning: Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘192.168.1.21‘ (113)

2)关闭防火墙测试连接,正常连接,然后重启slave ,复制正常


1. my.cnf 配置文件中 skip-networking 、bindaddress  被配置

skip-networking 导致所有TCP/IP端口没有被监听,其他客户端都无法用网络连接到本mysql服务器
bindaddress这个参数是mysql服务器只回应配置ip地址的请求连接


 


mysql主从复制环境搭建

标签:mysql   主从复制、   

原文地址:http://freebile.blog.51cto.com/447744/1656519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!