ubuntu下的mysql主从配置:
说明:由于MySQL不同版本之间的(二进制日志)binlog格式可能会不一样,因此最好的搭配组合是Master的MySQL版本和Slave的版本相同或者更低,
Master的版本肯定不能高于Slave版本。(版本向下兼容)
mysql1 : 129.1.23.8 //master
mysql2 : 129.1.23.246 //slave
安装步骤
1. 分别在两台机器上安装mysql-server
shell > apt-get install mysql-server
2.修改 mysql1 master 的配置文件
vim /etc/mysql/my.cnf
找到 bind-address = 127.0.0.1
改为 bind-address = 0.0.0.0
找到
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
去掉 注释符号
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
default-character-set = utf8 #新加上的为了保持编码一至防止出错
3.改好后保存退出,然后建立一个slave服务器的用户账号
root@msyql1:/# mysql -uroot -p
mysql> grant replication slave,replication client on *.* to slave@192.168.157.142 identified by '12345678';
mysql > grant replication slave on *.* to slave@192.168.157.142 identified by '12345678'; //给予权限
到这里要注意了,我的两台数据库都是空的.
重启mysql服务
4.修改 mysql2 服务器slave的 my.cnf配置文件
找到 bind-address = 127.0.0.1
替换 bind-address = 0.0.0.0
找到
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
把 注释符号去掉 改为如下
server-id = 2
master-host = 129.1.10.3
master-user = slave
master-password = ******
master-port = 3306
log_bin = /var/log/mysql/mysql-bin.log
log-slave-updates
skip-slave-start
//add
master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =test #只复制某个库
replicate-ignore-db=mysql #不复制某个库
配置完后 重新启动mysql
然后进入 mysql1 master 服务器
root@msyql:~# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show master status;
记录下来以后 进入 mysql2 slave mysql
root@msyql2:~# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> change master to master_log_file='mysql-bin.000009', master_log_pos=106;
//这个地方就是记录下来的 mysql1 master 的数据***********************************
mysql > start slave; //启动slave 服务
mysql > show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 129.1.23.8
Master_User: sx
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 884
Relay_Log_File: mysqld-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 884
Relay_Log_Space: 106
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
这一块有一个错误: Slave_IO_Running: No
Slave_SQL_Running: Yes
正常的话,应该是这样的:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Slave_IO_Running: No
解决办法:
先stop slave 再reset slave再start slave就正常了。
Slave_SQL_Running: No
解决办法:
1.首先停掉Slave服务:slave stop
2.到主服务器上查看主机状态:记录File和Position对应的值。
3.到slave服务器上执行手动同步:
change master to master_host='192.168.157.143', master_user='slave', master_password='12345678', master_port=3306, master_log_file='mysql-bin.000013', master_log_pos=106;
slave start
show slave status\G;