温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL主从复制的配置及原理

发布时间:2020-08-03 08:15:37 来源:网络 阅读:497 作者:damiahu 栏目:数据库

-----------------------------------------------------------

mysql 5.5   环境用的数据库可的多实例

-----------------------------------------------------------

mysql 主从复制原理图

MySQL主从复制的配置及原理

mysql 主从复制开始配置  环境是一个数据库多个实例 3306端口为主 3307端口为从

1 开始配置主库和从库的my.cnf配置文件

配置主数据库的my.cnf文件(3306)

server-id       = 1

log-bin=mysql-bin
配置从数据库的my.cnf文件(3307)

server-id       = 2

#log-bin=mysql-bin

然后重启数据库

2登录主数据库给从数据库分配权限用户

grant replication slave on *.* to 'repl'@'192.168.1.17' identified by '123456';

3 锁表并且导出主库里的数据

 flush table with read lock;

show master status;查看bin-log的位置状态

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000009 |      264 |              |                  |
+------------------+----------+--------------+------------------+


/usr/local/mysql/bin/mysqldump  -uroot -plampol -S /tmp/mysql3306.sock --events  -A  -B > /tmp/master.sql;

4 进从数据库导入主数据库的数据

source /tmp/master.sql

5进从库执行

change master to master_host='192.168.1.17',
      master_port=3306,
      master_user='repl',
      master_password='123456',
      master_log_file='mysql-bin.000009',
      master_log_pos=264;

6 从库开启同步开关 start slave

  • show slave status \G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.17
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000009
              Read_Master_Log_Pos: 500
                   Relay_Log_File: bogon-relay-bin.000002
                    Relay_Log_Pos: 489
            Relay_Master_Log_File: mysql-bin.000009
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes  //两个yes就ok了主从配置成功

                  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: 500
                  Relay_Log_Space: 645
                  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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI