搭建MySQL主从复制

安装环境:

两台centos 7虚拟机

一、建立时间同步环境,在主节点上搭建时间同步服务器

主服务器设置

1、安装ntp
[root@localhost~]#yum install ntp -y
2、配置ntp
[root@localhost~]#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,前两部分始终不变

[root@localhost~]#systemctl start ntpd.service
[root@localhost~]#systemctl stop firewalld.service
[root@localhost~]#setenforce 0
3、节点服务器设置:
[root@localhost~]#yum install ntp  ntpdate -y
[root@localhost~]#systemctl start ntpd.service
[root@localhost~]#systemctl stop firewalld.service
[root@localhost~]#systemctl stop firewalld.service
[root@localhost~]#setenforce 0
[root@localhost~]#/usr/sbin/ntpdate master_server IP   //进行时间同步

二、安装MySQL数据库,在Master和Slavel上安装

1、安装环境包:
[root@localhost~]#yum -y install \
ncurses \
ncurses-devel \
bison \
cmake \
gcc  \
gcc-c++
2、编译安装Mysql:
[root@localhost~]#useradd -s /sbin/nologin  mysql    //创建mysql用户
[root@localhost~]#mkdir /abc
[root@localhost~]#mount.cifs //vmnet8_ip  /abc
[root@localhost~]#cd /abc
[root@localhost abc]#tar zxvf  mysql-5.7.17.tar.gz -C /opt/
 [root@localhost abc]#tar zxvf boost_1_59_0.tar.gz -C /usr/local/
 [root@localhost abc]#cd /usr/local
 [root@localhost local]#mv boost_1_59_0 boost  //便于识别
 [root@localhost local]#cd opt/mysql-5.7.17/
 [root@localhost 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,否则错误依旧

[root@localhost mysql-5.7.17]#make && make install
[root@localhost mysql-5.7.17]#chown -R mysql.mysql /usr/local/mysql
[root@localhost 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_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

##### 3、优化调整
[root@localhost mysql-5.7.17]#chown mysql.mysql  /etc/my.cnf //更改属主、属组权限
[root@localhost mysql-5.7.17]#echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost mysql-5.7.17]#echo 'export PATH' >> /etc/profile
[root@localhost mysql-5.7.17]#source /etc/profile
##### 4、初始化数据库
[root@localhost mysql-5.7.17]#cd /usr/local/mysql/
[root@localhost mysql]#bin/mysqld \
--initialize-insecure \     //生成初始化的密码,为空,后面需要设置
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
[root@localhost mysql]#cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/    //方便系统识别管理
[root@localhost mysql]#systemctl daemon-reload   //刷新识别命令
##### 5、重启Mysql服务
 [root@localhost mysql]# systemctl restart mysqld
 [root@localhost mysql]# netstat -anpt | grep 3306  //查看端口是否开启
 [root@localhost mysql]#systemctl enable mysqld
 [root@localhost mysql]#mysqladmin -u root -p password "abc123" //给root账号设置密码为abc123提示输入的是原始密码
 [root@localhost 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服务,重新加载修改后的配置文件
[root@localhost ~]#systemctl mysqld.service
3)登录Mysql程序,给从服务器授权
[root@localhost ~]#mysql -u root -p

四、配置从服务器

1)在/etc/my.cnf中修改或增加以下内容
[root@localhost ~]#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服务
[root@localhost ~]#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)验证主从复制效果