附安装教学视频(高手请绕过,本教程仅适用于新手,有什么错误和不足的地方还请大家留言指正),链接如下:
https://www.bilibili.com/video/av36535481?share_medium=android&share_source=qq&bbid=F498D891-554A-48A7-9815-CC582ADF8DF327875infoc&ts=1542939432515
准备好两台搭建好mysql-8.0.13-linux-glibc2.12-x86_64的虚拟机
主服务器配置:
修改配置文件
在[mysqld]中添加如下字段
log-bin=mysql-bin
server-id=1
重启mysql服务器
service mysqld restart
配置或关闭主服务器的防火墙,因为要用从服务器连接
service firewalld stop
在主服务器master中新建从服务器用来复制日志文件的账户
create user ‘repl’@’IP’ identified by ‘password’;
说明:IP为从服务器的IP地址,’password’为从 服务器用来登录的密码
给新建账户授权
grant replication slave on *.* to ‘repl’@’IP’;
从服务器配置:
修改配置文件
在[mysqld]中添加如下字段
log-bin=mysql-bin
server-id=2
重启mysql服务
service mysqld restart
在主服务器上查看配置信息
show master status;
mysql> show master status
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
| mysql-bin.000001 | 711 | |
在从服务器上配置,并开启主从复制
配置:
change master to master_host=’IP’,master_user=’repl’,master_password=’password’,master_log_file=’’,master_log_pos=;
注:master_log_file,master_log_pos的内容根据第九步查出的内容进行填写
例:mysql> change master to master_host='192.168.174.130',master_user='repl',master_password='111',master_log_file='mysql-bin.000001',master_log_pos=711;
Query OK, 0 rows affected, 2 warnings (1.75 sec)
mysql> start slave;
Query OK, 0 rows affected (0.09 sec)
查看slave配置信息是否出错,如果出错解决错误后再进行测试。
show slave status \G
注:如果报下面的错误,请先在从服务器上用主从复制账号连接主服务器,成功连接后,再进行主从复制测试
mysql> show slave status \G
报错提示信息如下:
Last_IO_Errno: 2061
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 1
成功后的完整显示如下:
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.174.130
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 711
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql-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: 711
Relay_Log_Space: 534
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: 1
Master_UUID: a4382f20-ee42-11e8-99ff-000c293cb1c4
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
1 row in set (0.00 sec)
主从复制测试
在主服务器上进行测试,在从服务器上进行查看结果
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create databases info;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databases info' at line 1
mysql> create database info;
Query OK, 1 row affected (0.15 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| info |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
从服务器上显示结果:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.71 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| info |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+