在企业网站中,后端 MySQL 数据库只有一台时,会有以下问题:
首先要部署主从复制,只有主从复制完成了,才能在此基础上进行数据的读写分离
关闭防火墙,关闭核心防护等
(1)在主机 Master 搭建时间同步服务器 NTP(20.0.0.21)
[root@localhost ~]# yum -y install ntp
[root@localhost ~]# vi /etc/ntp.conf ###最后添加如下两行
server 127.127.1.0
fudge 127.127.1.0 stratum 8
[root@localhost ~]# service ntpd restart ###重启服务
[root@localhost ~]# systemctl restart ntpd
[root@localhost ~]# systemctl enable ntpd
(2) 在从服务器上配置 NTP 同步
登录到从服务器(20.0.0.23)
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.21
14 Sep 22:54:37 ntpdate[10738]: adjust time server 20.0.0.21 offset -0.470032 sec
[root@localhost~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.21 >>/var/log/ntpdate.log
[root@localhost~]# systemctl restart ntpdate
[root@localhost~]# systemctl enable ntpdate
[root@localhost~]# tail -f /var/log/ntpdate.log ###动态查看更新日志文件
登录到从服务器(20.0.0.25)
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.21
14 Sep 22:55:05 ntpdate[9726]: step time server 20.0.0.21 offset -0.808074 sec
[root@localhost~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.21 >>/var/log/ntpdate.log
[root@localhost~]# systemctl restart ntpdate
[root@localhost~]# systemctl enable ntpdate
[root@localhost~]# tail -f /var/log/ntpdate.log
###编译安装###
[root@localhost~]# yum -y install \
ncurses \
ncurses-devel \
bison \
cmake \
gcc \
gcc-c++
[root@localhost~]# useradd -s /sbin/nologin mysql
[root@localhost~]# cd /opt
[root@localhost opt]# tar xf mysql-boost-5.7.20.tar.gz
[root@localhost opt]# cd /opt/mysql-5.7.20/
[root@localhost mysql-5.7.20]#
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=boost \
-DWITH_SYSTEMD=1
[root@localhost mysql-5.7.20]# make -j4
[root@localhost mysql-5.7.20]# make install
[root@localhost mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/ ###数据库目录进行权限调整
[root@localhost mysl-5.7.20]# vi /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
[root@localhost mysql-5.7.20]# chown mysql:mysql /etc/my.cnf
[root@localhost mysql-5.7.20]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost mysql-5.7.20]# echo 'export PATH' >> /etc/profile ###设置环境变量
[root@localhost mysql-5.7.20]# source /etc/profile ###生效
[root@localhost mysql-5.7.20]# 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 enable mysqld
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# systemctl status mysqld
[root@localhost mysql]# netstat -anpt | grep 3306
[root@localhost mysql]# mysqladmin -u root -p password ###设置密码
[root@localhost~]# vi /etc/my.cnf
server-id = 11 ###标识
log_bin = master-bin
log-slave-updates = true
[root@localhost~]# systemctl restart mysqld
[root@localhost~]# mysql -uroot -p
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by 'abc123';
mysql> flush privileges;
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 599 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@localhost ~]# vim /etc/my.cnf
server-id = 22 ###修改
relay-log=relay-log-bin ###增加下两行
relay-log-index=slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld.service
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> change master to master_host='20.0.0.21',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=599;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 20.0.0.21
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 599
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes //开启
Slave_SQL_Running: Yes //开启
Replicate_Do_DB:
Replicate_Ignore_DB:
[root@localhost ~]# vim /etc/my.cnf
server-id = 33 ###修改
relay-log=relay-log-bin ###增加下两行
relay-log-index=slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld.service
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> change master to master_host='20.0.0.21',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=599;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 20.0.0.21
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 599
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes //开启
Slave_SQL_Running: Yes //开启
Replicate_Do_DB:
Replicate_Ignore_DB:
主服务器(20.0.0.21):
[root@localhost mysql]# mysql -uroot -p
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
从服务器 1(20.0.0.23):
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test | ###存在test数据库
+--------------------+
5 rows in set (0.00 sec)
从服务器 2(20.0.0.25):
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test | ###存在test数据库
+--------------------+
5 rows in set (0.00 sec)