记录一下步骤:
master配置
前提:保证用的mysql版本一致:
1.修改MYSQL配置文件/etc/my.cnf,并重启;
log-bin=mysql-bin //必须
server-id=1 //必须,这里用于标识唯一的数据库,可以设置为IP地址后两位
binlog-ignore-db=information_schema //不同步的库
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=ufind_db
binlog-ignore-db:表示同步的时候ignore的数据库 binlog-do-db:指定需要同步的数据库
2.进入mysql,创建从库的授权用户,允许该用户在主库上读取日志;
>GRANT REPLICATION SLAVEON
*.*TO'root'@'192.168.1.2'
IDENTIFIED BY'mysql
password';
>FLUSH PRIVILEGES
3.显示主库信息
>show master status;
这里的 File 、Position 是在配置Salve的时候要使用到的,Binlog_Do_DB表示要同步的数据库,Binlog_Ignore_DB 表示Ignore的数据库,这些都是在配置的时候进行指定的。
另外:如果执行这个步骤始终为Empty set(0.00 sec)
,那说明前面的my.cnf没配置对。
slave库配置
1.修改mysql配置文件/etc/my.cnf 并重启;
log-bin=mysql-bin //不是必须的
server-id=3 //必须的
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=ufind_db
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
2.进入mysql
mysql> stop slave; #关闭Slave
mysql> change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000004', master_log_pos=28125;
mysql> start slave; #开启Slave
在这里指定Master的信息,master_log_file是在配置Master的时候的File选项, master_log_pos是在配置Master的Position 选项,这里要进行对应。
然后可以通过mysql> show slave status;
查看配置的信息:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.167.1.1
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 28125
Relay_Log_File: VM_128_194_centos-relay-bin.000004
Relay_Log_Pos: 26111
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: ufind_db
Replicate_Ignore_DB: mysql
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: 28125
Relay_Log_Space: 26296
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: 2
Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd
Master_Info_File: /data/mysqldb/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
可以看到,已经配置成功。我是对所有数据库进行的同步,如果是在配置文件中指定同步的数据库后,增加数据库需要在主和从的/etc/my.cnf文件中都要配置,并重启mysql,然后再从库的mysql中执行:
>stop slave
>change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000005', master_log_pos=120;
>start slave
上面的master_log_pos=120是表示从这个位置开始同步,新增的数据库才会同步到从库中,而原来主库的是不会同步的,所以在做主从之前可以先将主库中的所有数据库导出,然后导入从库中;
mysqldump -uroot -p --all-databases >bak.sql;
进入到从库的数据库中:
>source bak.sql