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

相关推荐

  • N25-Bazinga-第二周作业

    1.Linux文件管理类命令 命令 功能 命令 功能 pwd 显示当前目录 ls 显示目录下的内容 cd 改变所在目录 cat 显示文件的内容 grep 在文件中查找字符串 cp 复制文件 touch 创建文件 mv 移动文件 rm 删除文件 rmdir 删除目录 1.1 pwd命令 该命令的英文解释为print working&nbsp…

    Linux干货 2016-12-13
  • Linux 目录配置

    Linux 目录配置 Linux目录配置标准:FHS 因为利用Linux来开发产品或distributions的团队/公司与个人实在太多了,如果每个人都用自己的想法来配置文件放置的目录,那么将可能造成很多管理上的困扰。所以/后来就有所谓的Filesystem Hierarchy Standard (FHS)标准出炉了。 根据FHS(http://www.pa…

    Linux干货 2017-03-26
  • lvm逻辑卷管理

    #LVM 逻辑卷管理 一、创建pv        创建pv可以在物理硬盘(裸盘上创建),MBR类型的分区(要更改分区类型为linux lvm: 8e )。GPT分区也要更改分区类型为8e00 Linux LVM 。    还可以在RAID上创建。注意要在没有数据的分区上…

    Linux干货 2016-09-02
  • CentOS 7上的性能监控工具

    Linux中基于命令行的性能监控工具:dstat、top、netstat、vmstat、htop、ss、glances 1、dstat – 多类型资源统计工具(需配置epel源)   该命令整合了vmstat,iostat和ifstat三种命令。同时增加了新的特性和功能可以让你能及时看到各种的资源使用情况,从而能够使你对比和整…

    Linux干货 2016-09-07
  • Linux 用户, 组和权限

    用户, 组和权限 Linux登陆需要用户名、密码。/etc/passwd 文件保存用户名。登录linux时,Linux 先查找 /etc/passwd 文件中是否有这个用户名,没有则跳出,有则读取用户名的user ID 、 group ID 、用户名对应的根目录路径以及所使用的 shell ,最后在 /etc/shadow 中核对该 UI…

    2017-07-22
  • redis主从复制(4)— client buffer

    1、 client buffer的设计 redis server以单进程的方式处理接收到的请求,而redis完成请求有些工作比较慢,比如网络IO和磁盘IO等比较慢的操作。redis为了提高处理客户端请求的响应时间,做了很多优化。比如网络io和磁盘io是异步完成、使用后台进程完成bgsave和bgrewriteaof工作,在server端为客户提供读buffe…

    Linux干货 2016-03-28