Mysql主从同步
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql主从同步相关的知识,希望对你有一定的参考价值。
搭建mysql主从复制
安装环境:
两台centos 7虚拟机
一、建立时间同步环境,在主节点上搭建时间同步服务器
主服务器设置
1、安装ntp
[[email protected]~]#yum install ntp -y
2、配置ntp
[[email protected]~]#vim /etc/ntp.conf //添加下面两行
server 127.127.200.0 //本地时钟源
fudge 127.127.200.0 stratum 8 //设置时间层级为8(限制在15层以内)
注意:这里127.127.200.0一一对应主服务器的192.168.200.0,如果主服务器为192.168.100.0,则本地时钟源为127.127.100.0,前两部分始终不变
[[email protected]~]#systemctl start ntpd.service
[[email protected]~]#systemctl stop firewalld.service
[[email protected]~]#setenforce 0
3、节点服务器设置:
[[email protected]~]#yum install ntp ntpdate -y
[[email protected]~]#systemctl start ntpd.service
[[email protected]~]#systemctl stop firewalld.service
[[email protected]~]#systemctl stop firewalld.service
[[email protected]~]#setenforce 0
[[email protected]~]#/usr/sbin/ntpdate master_server IP //进行时间同步
二、安装MySQL数据库,在Master和Slavel上安装
1、安装环境包:
[[email protected]~]#yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++
2、编译安装Mysql:
[[email protected]~]#useradd -s /sbin/nologin mysql //创建mysql用户
[[email protected]~]#mkdir /abc
[[email protected]~]#mount.cifs //vmnet8_ip /abc
[[email protected]~]#cd /abc
[[email protected] abc]#tar zxvf mysql-5.7.17.tar.gz -C /opt/
[[email protected] abc]#tar zxvf boost_1_59_0.tar.gz -C /usr/local/
[[email protected] abc]#cd /usr/local
[[email protected] local]#mv boost_1_59_0 boost //便于识别
[[email protected] local]#cd opt/mysql-5.7.17/
[[email protected] mysql-5.7.17]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql //指定目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock //连接文件
-DSYSCONFDIR=/etc //配置文件目录
-DSYSTEMD_PID_DIR=/usr/local/mysql //进程文件
-DDEFAULT_CHARSET=utf8 //字符集
-DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data //数据库
-DWITH_BOOST=/usr/local/boost //支持c++库
-DWITH_SYSTEMD=1
注意:如果在CMAKE的过程中有报错,当报错解决后,需要把源码目录中的CMakeCache.txt文件删除,然后再重新CMAKE,否则错误依旧
[[email protected] mysql-5.7.17]#make && make install
[[email protected] mysql-5.7.17]#chown -R mysql.mysql /usr/local/mysql
[[email protected] mysql-5.7.17]#vim /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
![](http://i2.51cto.com/images/blog/201807/10/60aa994307427a5fc19112d1e2ed54d6.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
##### 3、优化调整
[[email protected] mysql-5.7.17]#chown mysql.mysql /etc/my.cnf //更改属主、属组权限
[[email protected] mysql-5.7.17]#echo ‘PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH‘ >> /etc/profile
[[email protected] mysql-5.7.17]#echo ‘export PATH‘ >> /etc/profile
[[email protected] mysql-5.7.17]#source /etc/profile
##### 4、初始化数据库
[[email protected] mysql-5.7.17]#cd /usr/local/mysql/
[[email protected] mysql]#bin/mysqld --initialize-insecure //生成初始化的密码,为空,后面需要设置
--user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[[email protected] mysql]#cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ //方便系统识别管理
[[email protected] mysql]#systemctl daemon-reload //刷新识别命令
##### 5、重启Mysql服务
[[email protected] mysql]# systemctl restart mysqld
[[email protected] mysql]# netstat -anpt | grep 3306 //查看端口是否开启
[[email protected] mysql]#systemctl enable mysqld
[[email protected] mysql]#mysqladmin -u root -p password "abc123" //给root账号设置密码为abc123提示输入的是原始密码
[[email protected] mysql]#mysql -u root -p
显示下图则表示数据库搭建成功
三、配置Mysql Master 主服务器
1)在 /etc/my.cnf中修改或者增加以下内容,在[mysqld]下配置
server-id =11 //主从服务器id不能相同
log-bin=master-bin //改为master
log-slave-updates=true //同步从服务器
2)重启Mysql服务,重新加载修改后的配置文件
[[email protected] ~]#systemctl mysqld.service
3)登录Mysql程序,给从服务器授权
[[email protected] ~]#mysql -u root -p
四、配置从服务器
1)在/etc/my.cnf中修改或增加以下内容
[[email protected] ~]#vim /etc/my.cnf
server-id = 22
relay-log=relay-log-bin //从主服务器上同步日志文件记录到本地//
relay-log-index=slave-relay-bin.index //定义relay-log的位置和名称//
这里注意server-id不能和主服务器相同
2)重启Mysql服务
[[email protected] ~]#systemctl mysqld.service
3)登录Mysql,配置同步
根据主服务器更改maste_log_file和master_log-pos的参数
mysal>
change master to master_host=‘master_server_ip‘,master_user=‘myslave‘,master_password=‘123456‘,master_log_file=‘master-bin.000001‘,master_log_pos=604;
4)启动同步
mysql>start salve;
5)查看Slave状态,确保以下两个值为YES
6)验证主从复制效果
以上是关于Mysql主从同步的主要内容,如果未能解决你的问题,请参考以下文章
web服务文件更新自动同步数据库主从复制shell脚本实现网站代码备份和mysql备份