本文主要解决 mysql主从两台服务器之间,数据实时同步。环境是debain系统,已安装mariadb。
主机192.168.0.1,从机192.168.0.5
1、在一台服务器配置为从机
在linux终端找到 /etc/mysql/mariadb.conf.d/50-server.cnf
将其 server-id 设为 2(默认为1),
然后重启MYSQL服务
systemctl restart mariadb.service
2、在主机上加一个从机可以登录的用户
root@# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 172138
Server version: 10.0.36-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>grant replication slave on *.* on 'sally'@'192.168.0.5' identified by 'ilovesally';
flush privileges;
3、建好后,在从机192.168.0.5 服务器上执行以下语句,如果可以连上,则进行下一步,
root@# mysql -h 192.168.0.1 -u sally -P 3306 -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 172155
Server version: 10.0.36-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
4、在主机执行以下语句,查看master状态
show master status;
5、在从机执行如下sql语句:
其中MASTER_LOG_FILE='mysql-bin.000020',MASTER_LOG_POS=1441;在第4步中取到
CHANGE MASTER TO
MASTER_HOST='192.168.0.1',
MASTER_USER='sally',
MASTER_PASSWORD='ilovesally',
MASTER_LOG_FILE='mysql-bin.000020',
MASTER_LOG_POS=1441;
6、在从库上继续执行如下sql语句:
slave start; show slave status;
这样,查看从服务器的状态,如果状态中的用红线标出来两个参数的值都为YES,那证明配置已经成功,否则可以检查一下具体问题出现在什么地方。
7、这样,就算配置完成了。在主库中新建数据库,新建一张表,插几条数据,到从库上查询一下看是否已经同步过来。