Chinaunix首页 | 论坛 | 博客
  • 博客访问: 610992
  • 博文数量: 244
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-27 09:53
个人简介

记录学习,记录成长

文章分类

全部博文(244)

我的朋友

分类: LINUX

2015-12-11 20:43:49

mysql主从复制之mysql5.5异步配置
配置环境
mysql主服务器:192.168.85.144
mysql从服务器:192.168.85.145
mysql版本:5.5.46

一.主服务器上
1.创建MYSQL数据存放目录(最好创建逻辑卷)
[root@node1 ~]# mkdir -pv /mysqldata/data
mkdir: created directory `/mysqldata'
mkdir: created directory `/mysqldata/data'

2.创建mysql用户
[root@node1 ~]# useradd -r mysql

3.更改数据目录属主属组
[root@node1 ~]# chown -R mysql.mysql /mysqldata/data/

4.解压mysql后创建链接目录并更改目录内文件的属主属组
[root@node1 ~]# tar xf mysql-5.5.46-linux2.6-i686.tar.gz -C /usr/local/

[root@node1 ~]# cd /usr/local/

[root@node1 local]# ln -sv mysql-5.5.46-linux2.6-i686 mysql
`mysql' -> `mysql-5.5.46-linux2.6-i686'

[root@node1 local]# cd mysql

[root@node1 mysql]# chown -R root.mysql ./*

5.初始化mysql
[root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mysqldata/data/

6.配置mysql
6.1创建配置文件
[root@node1 mysql]# cp support-files/my-large.cnf /etc/my.cnf

6.2创建启动文件
[root@node1 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@node1 mysql]# chkconfig --add mysqld

6.3编辑配置文件
要注意的是:
log-bin=master-bin #启用二进制日志功能
log-bin-index=master-bin.index
binlog_format=mixed #该值要么是mixed要么是raw,不要使用statement
datadir=/mysqldata/data #指定数据存放目录

6.4启动mysql服务
[root@node1 mysql]# service mysqld start
Starting MySQL...... SUCCESS! 

6.5导入环境变量
[root@node1 mysql]# export PATH=$PATH:/usr/local/mysql/bin
[root@node1 mysql]# echo $?
0

[root@node1 mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.46-log MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

7.创建主从复制时所用的帐号
mysql> grant replication slave on *.* to 'repuser'@'192.168.85.145' identified by 'reppasswd';
Query OK, 0 rows affected (0.07 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

至此,主服务器的配置全部完成

二.从服务器配置
1.创建MYSQL数据存放目录
[root@node2 ~]# mkdir -pv /mysqldata/data
mkdir: created directory `/mysqldata'
mkdir: created directory `/mysqldata/data'

2.创建mysql用户
[root@node2 ~]# useradd -r mysql

3.更改数据目录属主属组
[root@node2 ~]# chown -R mysql.mysql /mysqldata/data/

4.解压mysql后创建链接目录并更改目录内文件的属主属组
[root@node2 ~]# tar xf mysql-5.5.46-linux2.6-i686.tar.gz -C /usr/local/

[root@node2 ~]# cd /usr/local/

[root@node2 local]# ln -sv mysql-5.5.46-linux2.6-i686 mysql
`mysql' -> `mysql-5.5.46-linux2.6-i686'

[root@node2 local]# cd mysql

[root@node2 mysql]# chown -R root.mysql ./*

5.初始化mysql
[root@node2 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mysqldata/data/

6.配置mysql
6.1将主服务器上的配置文件和启动文件复制到从服务器上
[root@node1 ~]# scp -p /etc/my.cnf node2:/etc/
my.cnf                                                100% 4747     4.6KB/s   00:00  
  
[root@node1 ~]# scp -p /etc/rc.d/init.d/mysqld node2:/etc/rc.d/init.d/mysqld
mysqld                                                100%   11KB  10.6KB/s   00:00    

[root@node2 mysql]# chkconfig --add mysqld

6.2修改配置文件
注释以下两行: #此次测试仅为一主一从,而从服务器又不会将二进制日志传给另外的服务器,所以取消了二进制日志功能
log-bin=master-bin
log-bin-index=master-bin.index

添加以下行: #启用中继日志功能
relay-log = relog
relay-log-index = relog.index

更改的行: #不要让其值为1即可
server-id = 11

6.3启动mysql服务
[root@node2 mysql]# service mysqld start
Starting MySQL...... SUCCESS! 

6.4导入环境变量
[root@node2 mysql]# export PATH=$PATH:/usr/local/mysql/bin/

[root@node2 mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.46 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

7.从服务器连接主服务器
主服务器当前的二进制日志信息:(因为之前创建了用于主从复制的用户,产生了部分操作,所以position变为了343,但是这些没必要复制到从服务器上)
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      343 |              |                  |
+-------------------+----------+--------------+------------------+

从服务器上:具体可show change master to查看相关用法

mysql> change master to master_host='192.168.85.144',master_user='repuser',master_password='reppasswd',master_log_file='master-bin.000001',master_log_pos=343;

8.查看从服务器状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.85.144
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60 #无法连接时的重试间隔
              Master_Log_File: master-bin.000001 #当前读取的二进制日志文件
          Read_Master_Log_Pos: 343 #当前读取的二进制日志文件中的位置
               Relay_Log_File: relog.000001 #从服务器的中继日志文件
                Relay_Log_Pos: 4 #从服务器的中继日志文件中的位置
        Relay_Master_Log_File: master-bin.000001 #中继的主服务器的二进制日志文件,和Master_Log_File一致
             Slave_IO_Running: No #slave的IO进程运行状态
            Slave_SQL_Running: No #slave的SQL进程运行状态
              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: 343 #当前执行到的二进制日志文件的位置
              Relay_Log_Space: 107 #relay-log空间
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No #主服务器上是否允许使用SSL功能
           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: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0

9.启动从服务器
mysql> start slave;      #IO进程和SQL进程都启动,也可以逐个启动

再次查看状态:
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.85.144
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 343
               Relay_Log_File: relog.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes #IO进程启动
            Slave_SQL_Running: Yes #SQL进程启动
              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: 343
              Relay_Log_Space: 400
              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
 
三.测试
主服务器上:
mysql> create database mydb;

从服务器上查看:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+

四.补充
1.主服务器可以读写,但是为了减轻其压力,一般主服务器都只负责写操作,而从服务器只能进行读操作,所以,为了避免故障发生,还需设置从服务器只能读即为其加上读锁,这样不会妨碍主服务器向其写入数据,但是其他人无法对其进行写操作;
1.1手动锁定
打开一个mysql连接后执行flush tables with read lock即刷新所有表后设置读锁;

1.2通过参数锁定
mysql> show global variables like 'read_only';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | OFF   |
+---------------+-------+
将其值改为on即可,但是MYSQL重启之后又为OFF,所以,若想永久更改,将read_only=on写入配置文件再重启服务即可;

2.重启从服务器的mysql服务后,IO进程个SQL进程依然是YES状态的,而且第一次我们配置了连接主服务器的用户和密码,所以能连接上,但是上面重启了mysql服务,我们也没有配置连接的用户和密码,但是仍然能连接上,这是因为依赖/mysqldata/data下的文件:
[root@node2 data]# cat master.info
18
master-bin.000002
107
192.168.85.144
repuser
reppasswd
3306
60
0


0
1800.000

0
[root@node2 data]# cat relay-log.info
./relog.000009
254
master-bin.000002
107
上面记录了连接主服务器的信息和中继日志信息,每次重新配置mysql重启后,从服务器能从这两个文件中获取连接信息;

3.上面设置了读锁(read_only=on),但是它对具有SUPER权限的用户是不生效的,所以,管理员依然能写;

4.关于同步二进制日志
有时会遇到这样的情况:主服务器上某个事务已经提交,其二进制日志事件会写入到日志文件中,但是二进制日志有缓冲区,所以事务一提交可能仍有些事件在缓冲区中而未写入二进制日志中,此时如果恰好主服务器崩溃,从服务器上可能无法得到相关的事件,这意味着从服务器上可能无法执行完整的事务。这种情况下,需要主服务器的事务一提交就立即同步到二进制日志中,而不在缓冲区中停留,所以,这服务器上要设置:
mysql> set global sync_binlog = 1;
mysql> show global variables like 'sync_binlog';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sync_binlog   | 1     |
+---------------+-------+

mysql5.5半同步主从复制哦诶之:
http://blog.chinaunix.net/uid-30212356-id-5572666.html
阅读(758) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~