标签:复制 localhost str 技术 sql数据库 主从同步 mct c4c tab
搭建MySQL主从复制两台centos 7虚拟机
[root@localhost~]#yum install ntp -y
[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
[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 //进行时间同步
[root@localhost~]#yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++
[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_100,g_se,x_10,y_10,shadow_90,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
显示下图则表示数据库搭建成功
server-id =11 //主从服务器id不能相同
log-bin=master-bin //改为master
log-slave-updates=true //同步从服务器
[root@localhost ~]#systemctl mysqld.service
[root@localhost ~]#mysql -u root -p
四、配置从服务器
[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不能和主服务器相同
[root@localhost ~]#systemctl mysqld.service
根据主服务器更改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;
mysql>start salve;
标签:复制 localhost str 技术 sql数据库 主从同步 mct c4c tab
原文地址:http://blog.51cto.com/13687553/2139674