主:192.168.1.1
从:192.168.1.2
主数据库master修改:
1.修改mysql配置
找到主数据库的配置文件my.cnf(或者my.ini)
[mysqld] #开启二进制日志 log-bin=mysql-bin binlog_format=mixed #设置server-id 可以自己随便设置但是要保证和slave的id不一样 server-id=1 # 只同步test数据库,除此之外,其他不同步 binlog-do-db = test
2.重启mysql,创建用于同步的用户账号
打开mysql会话shell>mysql -hlocalhost -uroot -proot
创建用户并授权:用户:root1密码:123456
mysql> CREATE USER 'repl'@'192.168.1.2' IDENTIFIED BY 'slavepass';#创建用户 mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.2';#分配权限 mysql>flush privileges; #刷新权限
3.查看master状态,记录二进制文件名(mysql-bin.000002)和位置(547):
mysql > SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 547 | test | | +------------------+----------+--------------+------------------+
从服务器slave修改:
同样找到my.cnf配置文件,添加server-id
1.修改mysql配置
[mysqld]
#开启二进制日志 log-bin=mysql-bin binlog_format=mixed #设置server-id server-id=2 # 只同步test数据库,除此之外,其他不同步 binlog-do-db = test
2.关闭同步
mysql>slave stop;
3.重启mysql,打开mysql会话,执行同步SQL语句(需要主服务器主机名,登陆凭据,二进制文件的名称和位置):
mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.1.1', -> MASTER_USER='root1', -> MASTER_PASSWORD='123456', -> MASTER_LOG_FILE='mysql-bin.000002', -> MASTER_LOG_POS=547;
4.启动slave同步进程:
mysql>start slave;
5.查看slave状态:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.1 Master_User: root1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 11662 Relay_Log_File: mysqld-relay-bin.000023 Relay_Log_Pos: 11765 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB:
当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了