将主库的数据 变更同步到从库,从而保证主库和从库数据一致。
它的作用是 数据备份,失败迁移,读写分离,降低单库读写压力
主服务器上面的任何修改都会保存在二进制日志( Bin-log日志) 里面。 从服务器上面启动一个I/O线程, 连接到主服务器上面请求读取二进制(Bin-log)日志,然后把读取到的二进制日志写到本地的Realy-log(中继日志)里面。 从服务器上面同时开启一个SQL线程,读取Realy-log(中继日志),如果发现有更新立即把更新的内容在本机的数据库上面执行一遍。
1.准备服务器
2.配置主库
3.配置从库
4.测试主从复制
主库:192.168.231.153
从库:192.168.231.136
关闭防火墙与selinux
[mysqld]
log-bin=/var/log/mysql/mysql-bin #启用二进制文件日志记录
server-id=1 # 服务id,保证整个集群环境唯一
read-only=0 #1是只读,0是读写
# mkdir /var/log/mysql
# chown mysql.mysql /var/log/mysql
# systemctl restart mysqld
为了用户在从库中用来连接主库的账号
mysql> create user '用户名'@'ip' identified with mysql_native_password by '密码';
mysql> grant replication slave on *.* to '用户名'@'ip'
这是为用户分配主从复制的权限
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 313 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[mysqld]
server-id=2
read-only=1 #普通用户一般只有只读
# systemctl restart mysqld
此处登录的是你从库mysql的root用户登录的账号与密码,不是在主库创建的用户
mysql> change master to master_host='主库的ip',
master_user='连接主库用户名',
master_password='连接主库用户的密码',
master_log_file='日志文件名',
mastre_log_pos='日志位置';
日志位置就是在主库show master status查出来那个数字 ----313
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 313 | | | |
+------------------+----------+--------------+------------------+-------------------+
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.231.153 -----主库的ip
Master_User: itcast -----用来连接主库的用户名
Master_Port: 3306 ------端口
Connect_Retry: 60
Master_Log_File: mysql-bin.000001 -----二进制日志名字
Read_Master_Log_Pos: 313
Relay_Log_File: congku-relay-bin.000002 -----中继日志名字
Relay_Log_Pos: 479
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes --------主要看俩个IO线程是否成功启动
Slave_SQL_Running: Yes --------看SQL线程是否成功启动
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
主库!
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| company |
| db1 |
| mysql |
| performance_schema |
| school |
| sys |
| xian |
+--------------------+
8 rows in set (0.00 sec)
从库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xian |
+--------------------+
5 rows in set (0.00 sec)
这是因为配置主从复制是在主库已经存在这些库以后配置的
!!!!在主库创建库,创建表,写入数据
mysql> create database db2;
mysql> create table db2.zhucong(id int,name varchar(50),age int);
mysql> insert into zhucong(id,name,age) values(1,'aaa',18),
values(2,'bbb',19) ,(3,'ccc',20);
mysql> select * from zhucong;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | aaa | 18 |
| 2 | bbb | 19 |
| 3 | ccc | 20 |
+------+------+------+
3 rows in set (0.00 sec)
!!!在从库查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db2 |
| mysql |
| performance_schema |
| sys |
| xian |
+--------------------+
mysql> use db2;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------+
| Tables_in_db2 |
+---------------+
| zhucong |
+---------------+
1 row in set (0.00 sec)
mysql> select * from zhucong;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | aaa | 18 |
| 2 | bbb | 19 |
| 3 | ccc | 20 |
+------+------+------+
修改主库中的表数据,查看从库是否有变化
主库:
mysql> update zhucong set age=21;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from zhucong;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | aaa | 21 |
| 2 | bbb | 21 |
| 3 | ccc | 21 |
+------+------+------+
3 rows in set (0.00 sec)
查看从库:
mysql> select * from zhucong;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | aaa | 21 |
| 2 | bbb | 21 |
| 3 | ccc | 21 |
+------+------+------+
3 rows in set (0.00 sec)
配置好主从复制以后,从库记录的只是主库以后的日志文件,要想从库备份主库之前的可以,先将主库之前的数据库文件备份然后拷贝到从库,然后从库在进行恢复即可。