主数据库:192.168.1.31/24
vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data/
socket=/tmp/mysql.sock
#master and slave
#启用二进制日志
log-bin=mysql-bin-master
#本机数据库ID 标示
server-id=1
#可以被从服务器复制的库,二进制日志需要同步的数据库名
binlog-do-db=test
#不可以被从服务器复制的库
binlog-ignore-db=mysql
[mysqld_safe]
log-error=/usr/local/mysql/log/err.log
pid-file=/usr/local/mysql/data/mysql.pid
重启/etc/init.d/mysqld restart
登录数据库授权一个子账号,让从数据库能够来到主同步二进制日志的信息
mysql> grant replication slave on . to [email protected] identified by "123456";
mysql> flush privileges; #刷新一下权限
mysql> select * from mysql.user where user=‘slave‘\G; #可以省略,这是查看授权给哪个号的权限
从数据库:192.168.1.32/24
vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data/
socket=/tmp/mysql.sock
#master and slave
#只设置一行ID就行了然后保存
server-id=2
[mysqld_safe]
log-error=/usr/local/mysql/log/err.log
pid-file=/usr/local/mysql/data/mysql.pid
重启/etc/init.d/mysqld restart
测试能不能登录192.168.1.31/24的主数据库,可以省略
mysql -uslave -p123456 -h192.168.1.31 结果可以登录OK
登录从库,停止slave
mysql> stop slave;
授权主库
mysql>change master to master_host=‘192.168.1.31‘,master_port=3306,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-bin-master.000001‘,master_log_pos=602;
开启slave
mysql>start slave
mysql>show slave status \G #查看主从有没有做成功
看到这两个线程就证明主从做成功了!
Slave_IO_Running:YES
Slave_SQL_Running:YES
原文地址:https://blog.51cto.com/kangxi/2422931