MysqlMariadb数据库主从复制


 

Mysql主从复制的实现原理图大致如下:

1.jpg

 

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;

# yum install mariadb-server.x86_64 mariadb.x86_64  -y

2.png

 

//设置mariadb服务

# systemctl start mariadb.service &&  systemctl enable mariadb.service

 

//设置mariadb数据库root账号的密码,默认root用户是没有密码;

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

 

In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

Enter current password for root (enter for none):

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n] y

 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n

 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

Remove test database and access to it? [Y/n] n

 ... skipping.

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

 

Reload privilege tables now? [Y/n] y

 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

 

二、主数据库master修改:

1.修改mysql配置

找到主数据库的配置文件my.cnf(或者my.ini),我的在/etc/my.cnf,在[mysqld]部分插入如下两行:

# find / -name my.cnf

clipboard.png

默认配置

clipboard.png

[mysqld]log-bin=mysql-bin #开启二进制日志 server-id=1 #设置server-id

 log-bin="/var/lib/mysql/" #设定生成的log文件名; 

修改后:

clipboard.png

# systemctl restart mariadb.service

 

2.重启mysql,创建用于同步的用户账号

# mysql -hlocalhost -uroot -ppassword

创建用户并授权:用户:wxp,密码:password

MariaDB [(none)]> CREATE USER 'wxp'@'192.168.3.218' IDENTIFIED BY 'password';#创建用户

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'wxp'@'192.168.3.218';#分配权限

 MariaDB [(none)]>flush privileges;    #刷新权限

clipboard.png

 

3.查看master状态,记录二进制文件名(mysql-bin.000001)和位置(492):

MariaDB [(none)]> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 |      492 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

clipboard.png

 

二、从服务器slave修改:

1.修改mysql配置

同样找到my.cnf配置文件,添加server-id

# find / -name my.cnf

my.cnf默认配置

clipboard.png

[mysqld]server-id=2             #设置server-id,必须唯一

 log-bin="/var/lib/mysql/" #设定生成的log文件名; 

修改后:

clipboard.png

# systemctl restart mariadb.service

 

2.重启mysql,打开mysql会话,执行同步SQL语句(需要主服务器主机名,登陆凭据,二进制文件的名称和位置):

# mysql -hlocalhost -uroot -ppassword

MariaDB [(none)]> CHANGE MASTER TO      -> MASTER_HOST='192.168.3.91',     -> MASTER_USER='wxp',     -> MASTER_PASSWORD='password',     -> MASTER_LOG_FILE='mysql-bin.000001',     -> MASTER_LOG_POS=492;

这里是直接把信息写入到数据库里面,

clipboard.png

 

mysql> select * from mysql.slave_master_info \G

 

3.启动slave同步进程:

MariaDB [(none)]>start slave;

clipboard.png

 

4.查看slave状态:

MariaDB [(none)]> show slave status\G;

MariaDB [(none)]> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.3.91

                  Master_User: wxp

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 492

               Relay_Log_File: mariadb-relay-bin.000002

                Relay_Log_Pos: 529

        Relay_Master_Log_File: mysql-bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: 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: 492

              Relay_Log_Space: 825

              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)

ERROR: No query specified

 

当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了。接下来就可以进行一些验证了,比如在主master数据库的test数据库的一张表中插入一条数据,在slave的test库的相同数据表中查看是否有新增的数据即可验证主从复制功能是否有效,还可以关闭slave(MariaDB [(none)]>stop slave;),然后再修改master,看slave是否也相应修改(停止slave后,master的修改不会同步到slave),就可以完成主从复制功能的验证了。

 

5、测试,操作Master数据库

MariaDB [(none)]> use test;

Database changed

MariaDB [test]> create table t1(Name varchar(18));

Query OK, 0 rows affected (0.03 sec)

 

MariaDB [test]> insert into t1(Name) values('wxp');

Query OK, 1 row affected (0.01 sec)

 

MariaDB [test]> select * from t1;

+------+

| Name |

+------+

| wxp  |

+------+

1 row in set (0.00 sec)

clipboard.png

 

在slave上面查看test库是否有数据同步过来;

[root@backup-3-218 ~]# mysql -hlocalhost -uroot -ppassword

MariaDB [(none)]> use test;

MariaDB [test]> show tables;

+----------------+

| Tables_in_test |

+----------------+

| t1             |

+----------------+

1 row in set (0.00 sec)

 

MariaDB [test]> select * from t1;

+------+

| Name |

+------+

| wxp  |

+------+

1 row in set (0.00 sec)

clipboard.png

 

6、还可以用到的其他相关参数:

master开启二进制日志后默认记录所有库所有表的操作,可以通过配置来指定只记录指定的数据库甚至指定的表的操作,具体在mysql配置文件的[mysqld]可添加修改如下选项:

# 不同步哪些数据库 

# vim /etc/my.cnf binlog-ignore-db = mysql   binlog-ignore-db = test   binlog-ignore-db = information_schema  

 

clipboard.png

# systemctl restart mariadb.service

   # 只同步哪些数据库,除此之外,其他不同步  binlog-do-db = wxp

 

# 日志保留时间

expire_logs_days = 10

 

# 控制binlog的写入频率。每执行多少次事务写入一次

# 这个参数性能消耗很大,但可减小MySQL崩溃造成的损失

sync_binlog = 5

 

# 日志格式,建议mixed

# statement 保存SQL语句

# row 保存影响记录数据

# mixed 前面两种的结合

binlog_format = mixed

 

在slave数据库上面操作,设置重新连接超时时间

# 停止主从同步

mysql> stop slave;

 

# 连接断开时,重新连接超时时间

mysql> change master to master_connect_retry=50;

 

# 开启主从同步

mysql> start slave;


原文地址:https://blog.51cto.com/14047707/2368267

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


安装开始...1.打开“https://dev.mysql.com/downloadsepo/yum/”下载Mysql源      将下载好的mysql源上传linux服务器 2.yumlocalinstallmysql80*#安装源 centos7中默认安装的是mariadb数据库,如果想要安装mysql,首先要移除mariadb;
安装Helm3#官网下载慢#wgethttps://get.helm.sh/helm-v3.5.4-linux-amd64.tar.gzwgethttp://qiniu.dev-share.top/helm-v3.5.4-linux-amd64.tar.gztar-zxvfhelm-v3.5.4-linux-amd64.tar.gzcplinux-amd64/helm/usr/local/bin#查看helmclient版本helmversion
通过Linux命令行启动用的指令:systemctlstartmariadb.service反馈:Failedtostartmariadb.service:Unitmariadb.servicenotfound.MariaDB简介MariaDB是MySQL的一个分支,MariaDB打算保持与MySQL的高度兼容性,确保具有库二进制奇偶校验的直接替换功能,以及与MySQLAPI和命令
InstallingMariaDBServer10.4TodeployMariaDBCommunityServer10.4onRHEL7orCentOS7,firstdownloadandusethe mariadb_repo_setup scripttoconfiguretheMariaDBrepositoriesforYUM:$sudoyuminstallwget$wgethttps://downloads.mariadb.com/
阅读目录一什么是存储引擎二mysql支持的存储引擎三使用存储引擎一什么是存储引擎mysql中建立的库--> 文件夹库中建立的表--> 文件现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型,处理表格用excel,处理图片
1、安装MariaDB安装命令yum-yinstallmariadbmariadb-server安装完成MariaDB,首先启动MariaDBsystemctlstartmariadb设置开机启动systemctlenablemariadb[root@node1~]#systemctlenablemariadbCreatedsymlinkfrom/etc/systemd/system/multi-user.target.wants/m
Centos7.5 刚安装的mariadb数据库报错报错:ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)原因一:没有启动数据库解决方法:systemctlstartmariadbsystemctlenablemariadb原因二:未知解决方法:[root@db01~]#
基于YUM安装的mariadb多实例.=================================================================1.yum安装mariadb-server包#yuminstallmariadb-server2.创建多实例对应的目录结构#mkdir/mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}-pv#tree/mysql//mysql/
一、系统环境[root@localhost~]#cat/etcedhat-releaseCentOSLinuxrelease7.6.1810(Core)二、mysql安装#yuminstallmysqlmysql-servermysql-devel安装mysql-server失败,如下图:[root@localhost~]#yuminstallmysql-serverLoadedplugins:fastestmirrorLoadingm
数据库的选择两大点是:开源和跨平台,满足这三点MySQL、MongoDB和MariaDB。其中MariaDB是MySQL的分支,也是它的进阶产品,未来很有可能替代MySQL。与MySQL相比较,MariaDB更强的地方在于:Maria 存储引擎PBXT存储引擎XtraDB 存储引擎FederatedX 存储引擎更快的复制查询处理线
使用Navicat连接数据库时出现了 HostxxxisnotallowedtoconnecttothisMariaDbserver的情况。发现了是因为授权的问题,使得连接权限受阻。所以,只需要进入数据库中,给予其权限即可。具体解决代码如下:[root@localhost~]#mysql-uroot-pEnterpassword:#首先进入mys
1.临时表当绘画结束时,临时表会自动销毁,无法用showtables查看临时表。MariaDB[jason]>createtemporarytabletmp(prochar(30),citychar(30));QueryOK,0rowsaffected(0.01sec)MariaDB[jason]>insertintotmpvalues('shanghai','shanghai');QueryOK,1
为了看阳光我来到世上数据库介绍数据库是一个存放数据的仓库,目前市面上最流行的数据库大致氛围的两种,一种叫做关系型数据库,一种叫做非关系型数据库,而关系型数据库近期流行的即为mysql,mysql原本是一个开源的数据库后被oracle公司收购,开始进行服务收费,因此,市面上大多数免费
1,Linux上的mysql MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松
启动Mariadb前提需安装mariadb-server-安装mariadb-serveryuminstall-ymariadb-server-启动服务systemctlstartmariadb.service-添加到开机启动systemctlenablemariadb.service-安全设置,以及修改数据库管理员密码mysql_secure_installation-启
1、查看是否安装及可用安装:yumlist|grepmaria2、安装maria(依赖其他几个安装包):yuminstallmariadb-server.x86_64输入'y'继续安装直到安装结束3、检查是否安装:4、安装完后登录提示错误:mysql-uroot-p是由于安装完但是并未启动maria服务5、启动maria有以下几种方法:system
通过yum安装mariadb,并配置MySQL主从服务器主服务器:192.168.10.11从服务器:192.168.10.12#!/bin/bash#====================================================#Author:Mr.Song#CreateDate:2019-02-21#Description:autoconfigMySQLmaster&slave#=====================
实验:实现基于SSL加密的主从复制实验步骤:环境:三台主机,一台CA:200,一台master:150,一台slave:100平时都是在CA上帮用户生成私钥,在服务器上做的1CA,master,slave的证书相关文件mkdir/etc/my.cnf.d/sslcd/etc/my.cnf.d/sslopensslgenrsa2048>cakey.pemopensslreq-new-x509-k
MariaDB[db1]>select*fromstudent;+----+------+-----+--------+-------+|id|name|age|gender|phone|+----+------+-----+--------+-------+|1|a|20|m|119||2|b|20|m|120||3|c|20|m|110
过年了,在老家闲余时间想敲敲代码,发现在安装mariaDb的时候一直报错错误信息:Service‘MySQL’(MySQL)Faildtostart,Verifythatyouhavesuffcientprivilegestostartsystemservices.服务的MySQL(MySQL)启动错误,确认你有权限启动系统服务。记得多年前在使用sqlserver数