主数据库服务器在数据库中做,增删改查操作。从服务器会同步主服务数据库的数据,数据的一致性!!!
如果是在企业网站中,后端mysql数据库只要一台的话,会有许多的问题;
如:
主服务器:20.0.0.25 master
从服务器:20.0.0.26 | 20.0.0.27 slave1 slave2
做时间同步;
- ntp服务是可以共享自己的时间给别的服务器,在主服务器上做;
- ntpdate 用于同步ntp服务器的时间,也是就咱们主服务器上的时间;
时间同步,才可保证数据的一致性,同步性!
1.下载ntp服务
yum -y install ntp #主服务器搭建时间同步服务,ntp是本地服务器开启时间同步,别人可以与你同步时间
2.编辑配置文件
vi /etc/ntp.conf #在配置文件内添加
server 127.127.1.0
fudge 127.127.1.0 stratum 8
3. 开启ntp服务,设置开机自启
systemctl start ntpd
systemctl enable ntpd
1.下载ntpdate服务
yum -y install ntpdate
2.同步主服务器的ntp服务时间
ntpdate 20.0.0.25
14 Sep 09:45:42 ntpdate[9818]: step time server 20.0.0.25 offset 28799.455584 sec
3.设置计划任务
crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.25 >>/var/log/ntpdate.log
systemctl restart crond
systemctl enable crond
4.验证计划任务,是否同步
tail -f /var/log/ntpdate.log
14 Sep 10:18:07 ntpdate[10013]: adjust time server 20.0.0.25 offset 0.002264 sec
1.下载ntpdate服务
yum -y install ntpdate
2.同步主服务器的ntp服务时间
ntpdate 20.0.0.25
14 Sep 09:45:42 ntpdate[9818]: step time server 20.0.0.25 offset 28799.455584 sec
3.设置计划任务
crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.21 >>/var/log/ntpdate.log
systemctl restart crond
systemctl enable crond
4.验证计划任务,是否同步
tail -f /var/log/ntpdate.log
14 Sep 10:18:07 ntpdate[10013]: adjust time server 20.0.0.25 offset 0.002264 sec
mysql安装目录,看之前的博客链接;
记得编译安装环境:
我的博客—Mysql数据库安装地址,直接可点击!这是个超链接!超链接!!!
1.在配置文件/etc/my.cnf 内,[mysqld]配置下
server-id = 11 #之前的标识是1 改成11
log_bin = master-bin #生成二进制日志文件
log-slave-updates = true
2.重启服务
systemctl restart mysqld
3.进入数据库,给从服务器授权--刷新数据库--查看mater
mysql -u root -p
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
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)
1.在配置文件/etc/my.cnf 内,[mysqld]配置下
server-id = 22 #之前的标识改为22,避免冲突
relay-log = relay-log-bin #
relay-log-index = slave-relay-bin.index
2.重启服务
systemctl restart mysqld
3.进入mysql数据库做授权
mysql -u root -p
// 同步配置,二进制日志文件和pos值要和主服务器一致!!先查看下主服务的master
mysql> change master to master_host='20.0.0.25',
-> master_user='myslave',
-> master_password='abc123',
-> master_log_file='master-bin000001',
-> master_log_pos=599;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G #查看数据库slave信息
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 20.0.0.25
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.000004
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes //开启的I/O线程
Slave_SQL_Running: Yes //开启的sql线程
1.在配置文件/etc/my.cnf 内,[mysqld]配置下
server-id = 22 #之前的标识改为22,避免冲突
relay-log = relay-log-bin #
relay-log-index = slave-relay-bin.index
2.重启服务
systemctl restart mysqld
3.进入mysql数据库做授权
mysql -u root -p
// 同步配置,二进制日志文件和pos值要和主服务器一致!!先查看下主服务的master
mysql> change master to master_host='20.0.0.25',
-> master_user='myslave',
-> master_password='abc123',
-> master_log_file='master-bin000001',
-> master_log_pos=599;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G #查看数据库slave信息
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 20.0.0.25
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.000004
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes //开启的I/O线程
Slave_SQL_Running: Yes //开启的sql线程
1.在主服务器创建一个数据库aaa;
mysql -u root -p
mysql> create database aaa;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
2.在从服务器验证;
26验证:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| mysql |
| performance_schema |
| sys |
| uou |
| wow |
+--------------------+
7 rows in set (0.02 sec)
27从服务器验证:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| cl |
| mysql |
| performance_schema |
| sys |
| lol |
| wow |
+--------------------+
8 rows in set (0.01 sec)