先让两台服务器互通
确定两台都是有安装mysql的
实际环境可以让主机的防火墙给端口3306放行,这里 就直接关闭防火墙了
输入密码进入mysql 并向服务器中授权一个从数据库用户给从机,然后刷新一下数据库的状态
` mysql -uroot -p123.com`
授权
`grant replication slave on *.*to‘slvZ‘@‘192.168.2.%‘ identified by‘123.com‘;`
修改配置文件
`vim /etc/my.cnf`
``
log-bin=master-bin
log-slave-update=true
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 11
``
重启服务
`systemctl restart mysqld`
进入mysql
``
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 107 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
``
需要记住这里的编码
去到从机修改配置文件
``
vim /etc/my.cnf
server-id = 22
``
重启数据库
`systemctl restart mysqld`
进入数据库
``
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host=‘192.168.2.9‘,master_user=‘slvZ‘,master_password=‘123.com‘,master_log_file=‘master-bin.000001‘,master_log_pos=107;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
#主要是看着两行
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
``
之后测试一下主创建 从查看就好了