彩票网站开发制作平台软件,郑州做网站软件,长尾关键词挖掘词工具,dedecms 百度网站地图文章目录 MySQL完成主从复制教程准备#xff1a;原理#xff1a;步骤#xff1a; 推荐文章 MySQL完成主从复制教程
主从复制#xff08;也称 AB 复制#xff09;就是将一个服务器#xff08;主服务器#xff09;的数据复制到一个或多个MySQL数据库服务器#xff08;从… 文章目录 MySQL完成主从复制教程准备原理步骤 推荐文章 MySQL完成主从复制教程
主从复制也称 AB 复制就是将一个服务器主服务器的数据复制到一个或多个MySQL数据库服务器从服务器中。
可以根据配置指定复制哪个库哪个表数据。
可以想到复制是通过异步的。
准备
2台版本一致的服务器数据库一台作为主库、一台作为从库
我的两个数据库版本5.7
一台服务器搭建2个数据库实例教程http://t.csdnimg.cn/aaErM
原理 主库一旦变更数据就会写入二进制日志文件Binary log从库IO线程 I/O thread连接到主库读取二进制日志文件内容并写入自己的中继日志文件Realy log然后从库 SQL thread 定时检查中继日志 Realy log发现有更新的内容就自己的库执行一遍。
从服务器都会copy主服务器二进制日志的全部内容到副本然后从服务器设备负责决定应该执行副本中的哪些语句。
步骤 主库配置my.cnf文件指定唯一server-id [mysqld]
## 同一局域网内注意要唯一
server-id100
## 开启二进制日志功能可以随便取关键
log-binmysql-bin
## 复制过滤不需要备份的数据库不输出mysql库一般不同步
binlog-ignore-dbmysql
## 为每个session 分配的内存在事务过程中用来存储二进制日志的缓存
binlog_cache_size1M
## 主从复制的格式mixed,statement,row默认格式是statement
binlog_formatmixed从库配置my.cnf文件指定唯一server-id [mysqld]
## 设置server_id,注意要唯一
server-id102
## 开启二进制日志功能以备Slave作为其它Slave的Master时使用
log-binmysql-slave-bin
## relay_log配置中继日志
relay_logedu-mysql-relay-bin
##复制过滤不需要备份的数据库不输出mysql库一般不同步
binlog-ignore-dbmysql
## 如果需要同步函数或者存储过程
log_bin_trust_function_creatorstrue
## 为每个session 分配的内存在事务过程中用来存储二进制日志的缓存
binlog_cache_size1M
## 主从复制的格式mixed,statement,row默认格式是statement
binlog_formatmixed
## 跳过主从复制中遇到的所有错误或指定类型的错误避免slave端复制中断。
## 如1062错误是指一些主键重复1032错误是因为主从数据库数据不一致
slave_skip_errors1062重启3306、3307服务使配置生效 # 查看mysql 3306、3307进程
rootsongdanminserver:[/usr/local/mysql/3306/run]ps -ef | grep mysqld
root 8568 25795 0 13:40 pts/0 00:00:00 grep --colorauto mysqld
mysql 30201 1 0 Nov10 ? 00:07:13 /usr/local/mysql/bin/mysqld --defaults-file/usr/local/mysql/3306/my.cnf --usermysql
mysql 30473 1 0 Nov10 ? 00:07:46 /usr/local/mysql/bin/mysqld --defaults-file/usr/local/mysql/3307/my.cnf --usermysql
# 停止3306、3307进程服务
rootsongdanminserver:[/usr/local/mysql/3306/run]kill 30201
rootsongdanminserver:[/usr/local/mysql/3306/run]kill 30473
# 查看mysql进程
rootsongdanminserver:[/usr/local/mysql/3306/run]ps -ef | grep mysqld
root 8614 25795 0 13:41 pts/0 00:00:00 grep --colorauto mysqld
# 查看端口占用情况是否没有3306、3307
rootsongdanminserver:[/usr/local/mysql/3306/run]netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::8080 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 127.0.0.1:8005 :::* LISTEN
# 重新启动3306、3307
rootsongdanminserver:[/usr/local/mysql/3306]nohup /usr/local/mysql/bin/mysqld --defaults-file/usr/local/mysql/3306/my.cnf --usermysql
[1] 9413
rootsongdanminserver:[/usr/local/mysql/3306]nohup: ignoring input and appending output to ‘nohup.out’
^C
rootsongdanminserver:[/usr/local/mysql/3307]nohup /usr/local/mysql/bin/mysqld --defaults-file/usr/local/mysql/3307/my.cnf --usermysql
[2] 9457
rootsongdanminserver:[/usr/local/mysql/3307]nohup: ignoring input and appending output to ‘nohup.out’
^C
rootsongdanminserver:[/usr/local/mysql/3306]# 查看mysql 3306、3307进程
rootsongdanminserver:[/usr/local/mysql/3307]ps -ef | grep mysqld
mysql 9413 25795 0 13:52 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file/usr/local/mysql/3306/my.cnf --usermysql
mysql 9559 25795 1 13:54 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file/usr/local/mysql/3307/my.cnf --usermysql
root 9608 25795 0 13:54 pts/0 00:00:00 grep --colorauto mysqld登录主库授予从库连接主库并复制主库数据的权限刷新权限生效 # 登录主库3306
rootsongdanminserver:[/usr/local/mysql/3306]mysql -uroot -h127.0.0.1 -p -P3306
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.36-log MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type help; or \h for help. Type \c to clear the current input statement.# 授予从库3307的root用户在指定从库ip上从主库复制所有库、所有表数据的权限
mysql grant replication slave, replication client on *.* to root你的从库服务器ip identified by 你的从库root用户密码;
Query OK, 0 rows affected, 1 warning (0.00 sec)# 刷新权限
mysql flush privileges;
Query OK, 0 rows affected (0.00 sec)# 查看MySQL现在有哪些用户及对应的IP权限可以看到你刚授权的从库user,host
mysql select user,host from mysql.user;
-----------------------------
| user | host |
-----------------------------
| root | % |
| root | 121.41.59.91 |
| mysql.session | localhost |
| mysql.sys | localhost |
-----------------------------
4 rows in set (0.00 sec)# 查看主库3306的binlog文件名和位置
mysql show master status;
-------------------------------------------------------------------------------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
-------------------------------------------------------------------------------
| mysql-bin.000003 | 612 | | mysql | |
-------------------------------------------------------------------------------
1 row in set (0.00 sec)登录从库连接主库指定主库ip、主库为从库连接所创建的用户、密码、从库从主库哪个二进制文件的哪里开始读取数据等 # 登录从库3307
rootsongdanminserver:[/usr/local/mysql/3306]mysql -uroot -h127.0.0.1 -p -P3307
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.36-log MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type help; or \h for help. Type \c to clear the current input statement.# 配置连接主库3306
# change master to master_host主库服务器ip, master_userroot(上一步主库授权从库能进行复制的用户), master_password123456(上一步主库授权从库能进行复制的密码), master_port3306(主库端口), master_log_filemysql-bin.000002(上一步查看到的主库日志文件名称),master_log_pos2079(上一步查看到的主库日志文件位置);
mysql change master to master_host121.41.53.91, master_userroot, master_password123456, master_port3306, master_log_filemysql-bin.000003,master_log_pos612;
Query OK, 0 rows affected, 1 warning (0.02 sec)从库启动主从复制查看是否连接主库成功 # 启动从库复制主库
mysql start slave;
Query OK, 0 rows affected (0.00 sec)# 查看主从复制状态查看Slave_IO_Running、Slave_SQL_Running 是否为yes
mysql show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 121.41.53.91 # 绑定的主库ipMaster_User: root # 主库用户Master_Port: 3306 # 主库端口Connect_Retry: 60 Master_Log_File: mysql-bin.000003 # 主库同步日志从这读取主库数据Read_Master_Log_Pos: 612Relay_Log_File: edu-mysql-relay-bin.000002 # 从库中继日志Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000003Slave_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: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 612Relay_Log_Space: 531Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 100Master_UUID: cfb53930-7fa2-11ee-8ea9-00163e2859d2Master_Info_File: /usr/local/mysql/3307/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)# 停止从库复制主库
mysql stop slave;
Query OK, 0 rows affected (0.01 sec) 测试主从复制是否成功在主库建表插入数据查看从库是否也有 在主库执行操作 新增库新增表新增数据修改数据删除数据 查看从库3307是否发生变化 # 进入主库3306
rootsongdanminserver:[/usr/local/mysql/3306]mysql -uroot -h127.0.0.1 -p -P3306
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.36-log MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type help; or \h for help. Type \c to clear the current input statement.
# 新建数据库 my_test
mysql create database my_test;
Query OK, 1 row affected (0.00 sec)mysql use my_test;
Database changed
mysql CREATE TABLE test (- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,- name VARCHAR(30) NOT NULL,- email VARCHAR(50) NOT NULL,- reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP- );
Query OK, 0 rows affected (0.01 sec)
# 插入初识数据
mysql INSERT INTO test (name, email)- VALUES (John Doe, johnexample.com);
Query OK, 1 row affected (0.00 sec)# 查看从库变化
注意如果出现从库复制失败没有效果时候从库重新绑定主库执行6
造成这类问题的原因一般是在主从复制的时候基于创建表然后又去删除和操作了数据表或者表。
推荐文章
【史上最细教程】一台服务器上搭建2个MySQL实例