Mysql之主从复制

Mysql之主从复制

节点一

修改配置文件设置唯一ID开起二进制日志

[root@node1 ~]# vim /etc/my.cnf 增加以下内容
    [mysqld]
    log-bin=master_bin  开起二进制日志
    server_id=1     给主节点一个唯一的ID号
    innodb_file_per_table=on    innodb开起独立表空间
    skip_name_resolve=on    开启跳过主机名反解

启动服务创建有远程复制权限的账户

[root@node1 ~]# service mariadb start
[root@node1 ~]# mysql
MariaDB [(none)]> show global variables like '%log%';   查看二进制日志log_bin是否开启了
MariaDB [(none)]> show global variables like '%server%';    查看DI号是否为1
MariaDB [(none)]> show master logs; 查看主节点二进制日志的位置,从节点从主节点最后一个日志的位置开始复制
MariaDB [(none)]> grant replication slave,replication client on *.* to 'copy'@'192.168.%.%' identified by 'passwd';     创建并授权一个远程复制账号copy密码为passwd
MariaDB [(none)]> flush privileges; 刷新用户权限

节点二

修改配置文件设置唯一ID开起中继日志

[root@node2 ~]# vim /etc/my.cnf
    relay_log=relay_log 开起中继日志
    relay-log-index=relay-log.index 
    server_id=2     同样的也需要设置唯一的ID号
    innodb_file_per_table=on
    skip_name_resolve=on

[root@node2 ~]# service mariadb start
[root@node2 ~]# mysql
MariaDB [(none)]> show global variables like '%log%';   查看中继日志relay_log是否开起
MariaDB [(none)]> show global variables like '%server%';    查看ID号是否为2
主节点为192.168.1.107,远程复制账号为copy,密码为passwd,复制二进制日志的起始位置为000003的245处
MariaDB [(none)]> change master to master_host='192.168.1.107',master_user='copy',master_password='passwd',master_log_file='master_bin.000003',master_log_pos=245;
MariaDB [(none)]> start slave;  启动从节点复制线程


MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.107
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master_bin.000003
          Read_Master_Log_Pos: 491
               Relay_Log_File: relay_log.000003
                Relay_Log_Pos: 776
        Relay_Master_Log_File: master_bin.000003
             Slave_IO_Running: Yes  这两项必须为yes
            Slave_SQL_Running: Yes  这两项必须为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: 491
              Relay_Log_Space: 1064
              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)

注意

如果Slave_IO_Running不为yes的解决办法

如:ERROR 1201 (HY000)

MariaDB [(none)]> slave stop;   停止从节点
MariaDB [(none)]> reset slave;  重新设置从节点

查找设置有问题的地方重新给从节点授权

MariaDB [(none)]> change master to master_host='192.168.1.107',master_user='copy',master_password='passwd',master_log_file='master_bin.000003',master_log_pos=245;
MariaDB [(none)]> start slave;  启动从节点
MariaDB [(none)]> show slave status\G;  查看状态

注意从节点上一定不能进行写操作

验证

主节点

MariaDB [(none)]> create database msdb;
MariaDB [msdb]> create table xx (id int(4) not null auto_increment,name varchar(30) not null,primary key(id)) engine=innodb charset=utf8;
MariaDB [msdb]> insert into xx (id,name) values (1,'king');

从节点

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| msdb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
MariaDB [(none)]> use msdb;
MariaDB [msdb]> show tables;
+----------------+
| Tables_in_msdb |
+----------------+
| xx             |
+----------------+
MariaDB [msdb]> select * from xx;
+----+------+
| id | name |
+----+------+
|  1 | king |
+----+------+

原创文章,作者:N17_信风,如若转载,请注明出处:http://www.178linux.com/24870

(0)
N17_信风N17_信风
上一篇 2016-07-19 09:12
下一篇 2016-07-19 22:45

相关推荐

  • 浅谈TCP三次握手和四次分手

          TCP(Transmission Control Protocol传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议提供可靠的连接服务,采用三次握手确认建立一个连接,比如我们去访问一个网站,从输入网址到页面显示我们所想要浏览的内容,这个过程其中就包含了小编要说的三次握手和四次挥手。 一、首先我们来了解…

    2017-09-02
  • linux 启动管理

    1、Linux系统启动流程:POST 加电自检 — BIOS(Boot Sequence)–>MBR(bootloader,446)(加载前512字节后的驱动程序,进入/boot目录,加载内核)–>Kernel–>initrd–>(系统根路径 /)(ROOTFS)/sbin/…

    Linux干货 2017-09-03
  • Linux网络管理相关工具

      Linux本身提供了许多用于网络测试、网络排错、网络状态分析的工具,下面来看几个比较常用的工具。   一、ping命令   ping命令是利用ICMP协议还测试网络的连通性。   命令格式:ping [option] IP     常用选项:       -c #:…

    Linux干货 2016-01-14
  • Linux进程与计划任务

    Linux进程与计划任务 linux进程及作业管理 进程 内核的功用:进程管理、文件系统、网络功能、内存管理、驱动程序、安全功能等特权操作模式切换(理想状态):70%CPU时间用户模式+30%CPU时间内核模式进程(Process):是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的存在生命周期的基本单位,是操作系统结构的基础。在早期…

    Linux干货 2016-09-23
  • linux入门

    linux入门 Centos中分root用户和普通用户,root为超级管理员,几乎具有所有的系统控制 刚进入linux系统,Ctrl+Alt+F[1-6]可以切换虚拟终端  (tty) 图形终端:CentOS 6: Ctrl + Alt + F7       CentOS 7:在哪个终端启动,即位于哪个虚拟终端 伪终端…

    Linux干货 2017-02-16
  • Linux终端类型

    Linux中各种终端的解释 设备终端   键盘鼠标显示器 物理终端( /dev/console ) )   在Linux 系统中,计算机显示器通常被称为控制台终端(Console)。 虚拟终端(tty :teletypewriters, /dev/tty# # 为[1-6])   tty 可有n 个,Ctrl+Alt+…

    Linux干货 2016-10-13