master:192.168.128.20
slave: 192.168.128.30
mysql 源码安装,版本:5.7.17
这里,之间采用已经编译完成的mysql,之间解压到 /usr/local下即可使用,日常可以对主库,然后从库恢复备份即可
[root@db_master ~]# tar xf mysql.tar.gz -C /usr/local
[root@db_master ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile # 配置mysql 环境变量
[root@db_master ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #配置启动脚本
[root@db_master ~]# vim /etc/my.cnf
[mysqld]
log_bin=/backup/master #开启bin_log日志
log_bin_index=/backup/master
innodb_file_per_table=1 #设置独立表空间
basedir = /usr/local/mysql
datadir = /data #设置数据存放位置
port = 3306
server_id = 20 #server_id 主从库不能一样,一般采用ip地址最后面段地址
socket = /tmp/mysql.sock #指定套接字
[root@db_master ~]# mysqld --initialize --use=mysql
[root@db_master ~]# mysqld_safe --initialize --user=mysql
[root@db_master ~]# mysqladmin -p'Ch_S,Su=w0P1' password "redhat";
[root@db_master ~]# mysqldump -p'redhat' --all-databases --single-transaction --master-data=1 --flush-logs > `date +%F`-mysql-all.sql
[root@db_master ~]# ls
2018-10-27-mysql-all.sql
--all-databases 备份所有数据库文件
--master-data=1 –master-data可以为1也可以为2,为1则二进制日志中授权的一步不会被注释掉,为2则没有
--single-transaction
--flush-logs
[root@db_master ~]# scp 2018-10-27-mysql-all.sql 192.168.128.30:/root # 备份文件拷贝给从库
mysql> grant replication slave on *.* to 'rep'@'192.168.128.30 identified by 'redhat';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
从库测试授权账户是否可用:
[root@db_slave1 ~]# mysql -urep -p'redhat' -h 192.168.128.20 # 注意: 后面ip地址为主库ip,意思为从库通过授权账户‘rep’去连接主库
mysql> show master status\G
*************************** 1. row ***************************
File: master.000004
Position: 600 #bin_log 日志位置,从600 开始
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
[root@db_slave1 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data
port = 3306
server_id = 2 #主要修改server-id ,区分主和从的数据
socket = /tmp/mysql.sock
[root@db_slave1 ~]# mysql_safe --initialize --user=mysql
[root@db_slave1 ~]# ss -anpt|grep 3306
LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=12332,fd=22))
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>source /root/2018-10-27-mysql-all.sql #从库恢复数据,验证两边数据是否一致
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| teamsun |
+--------------------+
5 rows in set (0.00 sec)
[root@db_slave1 ~]# mysql -urep -p'redhat' -h 192.168.128.20 #从库用授权账户 rep ,登陆主库ip
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
[root@db_slave1 ~]# mysql -uroot -p'redhat'
mysql> change master to
-> master_host='192.168.128.53',
-> master_user='rep',
-> master_password='redhat',
-> master_port=3306,
-> master_log_file='master.000004', #如果采用source /root/2018-10-27-mysql-all.sql 方式导入数据库,change master to 不用写 log_file 和 log_pos, sql备份文件中已经指明了log_file 和log_pos
-> master_log_pos=600;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
6)确认主从同步是否生效
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.128.53
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master.000004
Read_Master_Log_Pos: 600
Relay_Log_File: db_slave1-relay-bin.000002
Relay_Log_Pos: 317
Relay_Master_Log_File: master.000004
Slave_IO_Running: Yes #主要确认 IO 和SQL 两个线程是否启用,如果yes 说明主从复制正常
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
……
主库上,给teamsun.t1表中插入几条数据,从库测试是否同步
mysql> select * from teamsun.t1;
+------+----------+
| id | name |
+------+----------+
| 1 | jekinser |
| 1 | tom |
+------+----------+
mysql> insert into teamsun.t1 values(3,'jack');
mysql> insert into teamsun.t1 values(4,'rose');
mysql> insert into teamsun.t1 values(5,'hanghang');
mysql> select * from teamsun.t1;
+------+----------+
| id | name |
+------+----------+
| 1 | jekinser |
| 1 | tom |
| 3 | jack |
| 4 | rose |
| 5 | hanghang |
+------+----------+
从库上测试,两边数据一致
mysql> select * from teamsun.t1;
+------+----------+
| id | name |
+------+----------+
| 1 | jekinser |
| 1 | tom |
| 3 | jack |
| 4 | rose |
| 5 | hanghang |
+------+----------+
5 rows in set (0.00 sec)
采用传统方式配置主从复制(M-S)的基本步骤:
1 . 主服务器上打开bin_log 和 server_id,
2 . 给从服务器授权一个账号,拥有 replication slave权限
3 . 备份主服务器mysql所有数据,同时设置 --master-data=1(备份文件中自动指定log_file和log_pos)
4 . 从服务器上测试,授权账号是否可以连接主库
5 . 从服务器 进入MySQL控制台,通过source 方式导入备份文件,使主,从数据库数据一致;
6 . 从服务器通过change master to ,5条语句(master_log_file 和 master_log_pos可以省略)配置主从同步
7 . 从上start slave 启动从服务器上角色
8 . show slave staus 查看 IO SQL线程,确认总从同步是否生效
#注意: 主从复制服务器中,只能在master 上写入数据,slave 上不允许写入数据,一旦写入两边会发生冲突,SQL线程中断,主从复制被破坏。