yum -y install gcc ncurses
主和从服务器:
1:关闭所有服务器的firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
主服务器:
2:建立时间同步环境
[root@localhost ~]# yum -y install ntp
[root@localhost ~]# vim /etc/ntp.conf
添加:
server 127.127.1.0
fudge 127.127.1.0 stratum 8
[root@localhost ~]# systemctl restart ntpd
[root@localhost ~]# systemctl enable ntpd
从服务器:
3:在从节点上进行时间同步
[root@localhost ~]# yum -y install ntp
[root@localhost ~]# ntpdate 192.168.1.10
主和从服务器:
4:在所有服务器上安装mysql数据库
步骤略
主服务器:
5:配置mysql master主服务器
[root@localhost ~]# vi /etc/my.cnf
在[mysqld]模块中修改或添加:
server-id=11 ##修改
log-bin=master-bin ##修改
master-bin.000001
log-slave-updates=true ##添加 (可不用添加)
注释:
系统默认采用基于语句的复制类型
1:基于语句的复制。 在主服务器上执行的 SQL 语句,在从服务器上执行同样的语句。配置:binlog_format = STATEMENT
2:基于行的复制。把改变的内容复制过去,而不是把命令在从服务器上执行一遍,从 MySQL 5.0开始支持,配置:binlog_format = ROW
3:混合类型的复制。默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制,配置:binlog_format = MIXED
log-slave-updates=true #Slave可以是其他 Slave 的 Master,从而扩散 Master 的更新
binlog-ignore-db=test #不记录指定的数据库的二进制日志
replicate-ignore-db=test #设置不需要同步的库
binlog_cache_size = 1M #日志缓存的大小
expire_logs_days=3 #自动过期清理日志的天数
以上参数在[mysqld]模块中设置
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -u root -p
mysql> grant replication slave on . to 'myslave'@'192.168.1.%' identified by '123456' ;
mysql> flush privileges;
mysql> show master status;
输出结果(如果不一样,下面的change命令要改成相应的):
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 337 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.01 sec)
主和从都要敲:grant all on . to test@'192.168.1.%' identified by '123.com';
不然待会客户端验证没有权限
从服务器:
6:从服务器的配置
[root@localhost ~]# vi /etc/my.cnf
在[mysqld]模块中修改或添加:
server-id = 22 ##修改,57行值不能和其他mysql服务器重复
注释:
--relay-log=name 中继日志的文件的名字
--relay-log-index=name MySQL slave 在启动时需要检查relay log index 文件中的relay log信息,此处定义该索引文件的名字
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -u root -p
mysql> stop slave;
mysql> change master to master_host='192.168.1.10',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=337;
start slave;
如果报:ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log
原因:由于mysql-relay-bin.index中仍然保存着旧relay日志文件的路径
重置slave就行:reset slave;
注释: Slave 的 IO 线程接收到信息后,将接收到的日志内容依次写入到 Slave 端的Relay Log文件(relay-log-bin.xxxxxx)的最末端,并将读取到的Master端的master-bin的文件名和位置记录到master- info文件中,以便在下一次读取的时候能够清楚的告诉Master“我需要从某个master-bin的哪个位置开始往后的日志内容,请发给我
mysql> start slave;
mysql> show slave status\G
* 1. row *
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.102
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 411
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 411
Relay_Log_Space: 461
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 11
Master_UUID: 167be460-3d4d-11e8-ad42-000c29ae7f64
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
7:验证主从复制
(1)在主从服务器上分别查询数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
(2)在主服务器上创建数据库
mysql> create database db_test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_test |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
(3)在从服务器上再次查询数据库,可以看到从服务器上也有了db_test数据库了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_test |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)