Mysql主从复制的实现原理图大致如下:
MySQL之间数据复制的基础是以二进制日志文件(binary log file)来实现的,一台MySQL数据库一旦启用二进制日志后,其作为master,它数据库中所有操作都会以“事件”的方式记录在二进制日志中,其他数据库作为slave通过一个I/O线程与主服务器保持通信,并监控master的二进制日志文件的变化,如果发现master二进制日志文件发生变化,则会把变化复制到自己的中继日志中,然后slave的一个SQL线程会把相关的“事件”执行到自己的数据库中,以此实现从数据库和主数据库的一致性,也就实现了主从复制。MySQL(MariaDB)具体详细的安装可以参考《Linux就该这么学》教程的第十八章节,里面内容写的非常详细,适合初学者,本文也比较适合企业应用。
实现MySQL主从复制配置要求:
主服务器:1、开启数据库二进制日志功能;2、配置数据库认证唯一服务id;3、获得主库的二进制日志文件名及位置;4、在主库上面创建一个用于主库和从库通信的用户账号,安全管理。
从服务器:1、在从库中配置唯一服务id;2、使用主库创建分配的用户账号读取主库的二进制日志;3、启用slave功能,用于主从通信。

一、准备工作:

1.主从数据库版本最好一致;
2.主从数据库内数据保持一致;
主数据库(master):192.168.3.91 /CentOS Linux release 7.5.1804 (Core)
从数据库( slave ) :192.168.3.218 /CentOS Linux release 7.5.1804 (Core)
注意:这里的主从都是通过yum源安装的mariadb 5.5.56;
  1. yum install mariadb-server.x86_64 mariadb.x86_64 -y
 
//设置mariadb服务
  1. systemctl start mariadb.service && systemctl enable mariadb.service
//设置mariadb数据库root账号的密码,默认root用户是没有密码;
  1. mysql_secure_installation
  1. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  1. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  1.  
  1. In order to log into MariaDB to secure it, we'll need the current
  1. password for the root user. If you've just installed MariaDB, and
  1. you haven't set the root password yet, the password will be blank,
  1. so you should just press enter here.
  1. Enter current password for root (enter for none):
  1. OK, successfully used password, moving on...
  1. Setting the root password ensures that nobody can log into the MariaDB
  1. root user without the proper authorisation.
  1. Set root password? [Y/n] y
  1. New password:
  1. Re-enter new password:
  1. Password updated successfully!
  1. Reloading privilege tables..
  1. ... Success!
  1. By default, a MariaDB installation has an anonymous user, allowing anyone
  1. to log into MariaDB without having to have a user account created for
  1. them. This is intended only for testing, and to make the installation
  1. go a bit smoother. You should remove them before moving into a
  1. production environment.
  1. Remove anonymous users? [Y/n] y
  1. ... Success!
  1. Normally, root should only be allowed to connect from 'localhost'. This
  1. ensures that someone cannot guess at the root password from the network.
  1. Disallow root login remotely? [Y/n] n
  1. ... skipping.
  1. By default, MariaDB comes with a database named 'test' that anyone can
  1. access. This is also intended only for testing, and should be removed
  1. before moving into a production environment.
  1. Remove test database and access to it? [Y/n] n
  1. ... skipping.
  1. Reloading the privilege tables will ensure that all changes made so far
  1. will take effect immediately.
  1.  
  1. Reload privilege tables now? [Y/n] y
  1. ... Success!
  1. Cleaning up...
  1. All done! If you've completed all of the above steps, your MariaDB
  1. installation should now be secure.
  1. Thanks for using MariaDB!

二、主数据库master修改:

1.修改mysql配置
找到主数据库的配置文件my.cnf(或者my.ini),我的在/etc/my.cnf,在[mysqld]部分插入如下两行:
  1. find / -name my.cnf
 
默认配置
 
  1. [mysqld]
  1. log-bin=mysql-bin #开启二进制日志
  1. server-id=1 #设置server-id
  1. log-bin="/var/lib/mysql/" #设定生成的log文件名;
修改后:
 
  1. systemctl restart mariadb.service
2.重启mysql,创建用于同步的用户账号
  1. mysql -hlocalhost -uroot -ppassword
创建用户并授权:用户:wxp,密码:password
  1. MariaDB [(none)]> CREATE USER 'wxp'@'192.168.1.200' IDENTIFIED BY 'password';#创建用户
  1. MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'wxp'@'192.168.1.100';#分配权限
  1. MariaDB [(none)]>flush privileges; #刷新权限
 
3.查看master状态,记录二进制文件名(mysql-bin.000001)和位置(492):
  1. MariaDB [(none)]> SHOW MASTER STATUS;
  1. +------------------+----------+--------------+------------------+
  1. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  1. +------------------+----------+--------------+------------------+
  1. | mysql-bin.000001 | 492 | | |
  1. +------------------+----------+--------------+------------------+
  1. 1 row in set (0.00 sec)
 

三、从服务器slave修改:

1.修改mysql配置
同样找到my.cnf配置文件,添加server-id
  1. find / -name my.cnf
my.cnf默认配置
 
  1. [mysqld]server-id=2 #设置server-id,必须唯一
  1. log-bin="/var/lib/mysql/" #设定生成的log文件名;
修改后:
  1. systemctl restart mariadb.service
  1. mysql -hlocalhost -uroot -ppassword
  1. MariaDB [(none)]> CHANGE MASTER TO
  1. -> MASTER_HOST='192.168.3.91',
  1. -> MASTER_USER='wxp',
  1. -> MASTER_PASSWORD='password',
  1. -> MASTER_LOG_FILE='mysql-bin.000001',
  1. -> MASTER_LOG_POS=492;
这里是直接把信息写入到数据库里面,
 
  1. mysql> select * from mysql.slave_master_info \G
3.启动slave同步进程:
  1. MariaDB [(none)]>start slave;
 
4.查看slave状态:
  1. MariaDB [(none)]> show slave status\G;
  1. MariaDB [(none)]> show slave status\G;
  1. *************************** 1. row ***************************
  1. Slave_IO_State: Waiting for master to send event
  1. Master_Host: 192.168.3.91
  1. Master_User: wxp
  1. Master_Port: 3306
  1. Connect_Retry: 60
  1. Master_Log_File: mysql-bin.000001
  1. Read_Master_Log_Pos: 492
  1. Relay_Log_File: mariadb-relay-bin.000002
  1. Relay_Log_Pos: 529
  1. Relay_Master_Log_File: mysql-bin.000001
  1. Slave_IO_Running: Yes
  1. Slave_SQL_Running: Yes
  1. Replicate_Do_DB:
  1. Replicate_Ignore_DB:
  1. Replicate_Do_Table:
  1. Replicate_Ignore_Table:
  1. Replicate_Wild_Do_Table:
  1. Replicate_Wild_Ignore_Table:
  1. Last_Errno: 0
  1. Last_Error:
  1. Skip_Counter: 0
  1. Exec_Master_Log_Pos: 492
  1. Relay_Log_Space: 825
  1. Until_Condition: None
  1. Until_Log_File:
  1. Until_Log_Pos: 0
  1. Master_SSL_Allowed: No
  1. Master_SSL_CA_File:
  1. Master_SSL_CA_Path:
  1. Master_SSL_Cert:
  1. Master_SSL_Cipher:
  1. Master_SSL_Key:
  1. Seconds_Behind_Master: 0
  1. Master_SSL_Verify_Server_Cert: No
  1. Last_IO_Errno: 0
  1. Last_IO_Error:
  1. Last_SQL_Errno: 0
  1. Last_SQL_Error:
  1. Replicate_Ignore_Server_Ids:
  1. Master_Server_Id: 1
  1. 1 row in set (0.00 sec)
  1. ERROR: No query specified
  1. Until_Log_File:
 
当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了。接下来就可以进行一些验证了,比如在主master数据库的test数据库的一张表中插入一条数据,在slave的test库的相同数据表中查看是否有新增的数据即可验证主从复制功能是否有效,还可以关闭slave(MariaDB [(none)]>stop slave;),然后再修改master,看slave是否也相应修改(停止slave后,master的修改不会同步到slave),就可以完成主从复制功能的验证了。
5、测试,操作Master数据库
  1. MariaDB [(none)]> use test;
  1. Database changed
  1. MariaDB [test]> create table t1(Name varchar(18));
  1. Query OK, 0 rows affected (0.03 sec)
  1.  
  1. MariaDB [test]> insert into t1(Name) values('wxp');
  1. Query OK, 1 row affected (0.01 sec)
  1.  
  1. MariaDB [test]> select * from t1;
  1. +------+
  1. | Name |
  1. +------+
  1. | wxp |
  1. +------+
  1. 1 row in set (0.00 sec)
 
在slave上面查看test库是否有数据同步过来;
  1. [root@backup-3-218 ~]# mysql -hlocalhost -uroot -ppassword
  1. MariaDB [(none)]> use test;
  1. MariaDB [test]> show tables;
  1. +----------------+
  1. | Tables_in_test |
  1. +----------------+
  1. | t1 |
  1. +----------------+
  1. 1 row in set (0.00 sec)
  1.  
  1. MariaDB [test]> select * from t1;
  1. +------+
  1. | Name |
  1. +------+
  1. | wxp |
  1. +------+
  1. 1 row in set (0.00 sec)
  1.  
 
6、还可以用到的其他相关参数:
master开启二进制日志后默认记录所有库所有表的操作,可以通过配置来指定只记录指定的数据库甚至指定的表的操作,具体在mysql配置文件的[mysqld]可添加修改如下选项:
  1. # 不同步哪些数据库
  1. # vim /etc/my.cnf
  1.  
  1. binlog-ignore-db = mysql
  1. binlog-ignore-db = test
  1. binlog-ignore-db = information_schema
  1.  
  1.  
  1. # systemctl restart mariadb.service
  1. # 只同步哪些数据库,除此之外,其他不同步 binlog-do-db = wxp
  1.  
  1. # 日志保留时间
  1. expire_logs_days = 10
  1.  
  1. # 控制binlog的写入频率。每执行多少次事务写入一次
  1. # 这个参数性能消耗很大,但可减小MySQL崩溃造成的损失
  1. sync_binlog = 5
  1.  
  1. # 日志格式,建议mixed
  1. # statement 保存SQL语句
  1. # row 保存影响记录数据
  1. # mixed 前面两种的结合
  1. binlog_format = mixed
 
在slave数据库上面操作,设置重新连接超时时间
  1. # 停止主从同步
  1. mysql> stop slave;
  1.  
  1. # 连接断开时,重新连接超时时间
  1. mysql> change master to master_connect_retry=50;
  1.  
  1. # 开启主从同步
  1. mysql> start slave;

Mysql(Mariadb)数据库主从的更多相关文章

  1. MySQL/MariaDB数据库的主从级联复制

      MySQL/MariaDB数据库的主从级联复制 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.主从复制类型概述 1>.主从复制 博主推荐阅读: https://ww ...

  2. MySQL/MariaDB数据库的Galera高可用性集群实战

      MySQL/MariaDB数据库的Galera高可用性集群实战 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Galera Cluster概述 1>.什么是Gale ...

  3. MySQL/MariaDB数据库的复制监控和维护

      MySQL/MariaDB数据库的复制监控和维护 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.清理日志 1>.删除指定日志文件名称之前的日志(也可用基于时间) M ...

  4. MySQL/MariaDB数据库的主主复制

      MySQL/MariaDB数据库的主主复制 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.主主复制概述 1>.什么是主主复制 所谓的主主复制,说白了就是两台节点互为 ...

  5. MySQL/MariaDB数据库的主从复制

     MySQL/MariaDB数据库的主从复制  作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL复制概述 1>.传统扩展方式 垂直扩展(也叫向上扩展,Sacle ...

  6. MySQL/MariaDB数据库的性能测试

      MySQL/MariaDB数据库的性能测试 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据库服务衡量指标 qps: query per second(每秒支持多少查询 ...

  7. MySQL/MariaDB数据库的MHA实现高可用实战

      MySQL/MariaDB数据库的MHA实现高可用实战 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL高可用常见的解决方案 1>.Multi-Master ...

  8. MySQL/MariaDB数据库的PROXY实现读写分离

    MySQL/MariaDB数据库的PROXY实现读写分离 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ProxySQL概述 1>.各家互联网公司读写分离的解决方案 m ...

  9. MySQL/MariaDB数据库的复制加密

      MySQL/MariaDB数据库的复制加密 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL的安全问题 1>.基于SSL复制 在默认的主从复制过程或远程连接 ...

  10. MySQL/MariaDB数据库的复制过滤器

      MySQL/MariaDB数据库的复制过滤器 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.复制过滤器概述 1>.复制器过滤器功能 让从节点仅复制指定的数据库,或指 ...

随机推荐

  1. .gitignore 模式匹配

    匹配模式前使用 / 表示根目录 匹配模式后使用 / 代表是目录(不是文件) 匹配模式前加 ! 表示取反 * 代表任意个字符 ? 匹配任意一个字符 ** 匹配任意级目录

  2. 关于RAID小结

    独立硬盘冗余阵列(RAID, Redundant Array of Independent Disks),旧称廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks ...

  3. php RSA和AES加密算法

    一.RSA加密 RSA只说PHP中的应用,详细的算法原理解释,请自行百度,或者参考(RSA加密算法-详细解释以及公钥加密为什么每次都不一样) 总结:公钥加密.私钥解密.私钥签名.公钥验签. 注意: 1 ...

  4. C语言合法标识符(hud2024)

    输入方式:先输入一个整型,再循环输入带空格的字符串. 思考:整型用scanf_s()输入.大循环输入字符串前用getchar()函数读取缓冲区的字符.然后,输入带空格的字符串就要用”gets_s()“ ...

  5. 正确去除隐藏在WordPress系统各处的版本号

    使用WordPress的博主都有一个普遍的意识,就是为了安全而移除WordPress的版本号,以免不良用心的人利用旧版本的漏洞对网站进行攻击. WordPress会在前端代码head中加入以下代码(3 ...

  6. 项目readme文件目录生成工具 treer

    生成目录的工具呢有tree和treer,但是tree不知道怎么忽略node_modules文件夹, 而treer就简单了,下面就是基本的命令了 其中-i是指忽略xxx, -e是指导出 安装 npm i ...

  7. MVC案例之模糊查询与删除

    查询操作: Servlet         //1. 调用 CustomerDAO 的 getAll() 得到 Customer 的集合 List<Customer> customers ...

  8. Docker: GUI 应用,macOS 上如何运行呢?

    操作系统: macOS Catalina 基础镜像: continuumio/anaconda3, based on debian Step 1) 安装 XQuartz,允许网络连接 # 安装 bre ...

  9. ES6-for...of与for...in

    1.includes 数组是否包含某个东西 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  10. Spring AMQP:RabbitTemplate SimpleMessageListenerContainer

    一.RabbitTemplate介绍 RabbitTemplate:消息模板,在与Spring AMQP整合时,进行发送消息的关键类. 包括了可靠性投递消息方法.回调监听消息接口ConfirmCall ...