Linux部署Mysql数据库主从同步

添加评论 2011年12月29日

搞了一天,最后没办法,在hostloc和im286还有微博里面求救,有一个hostloc的朋友(醒醒)加我QQ,帮了挺多的,再次感谢。

最后,我们实在找不到问题的所在,我还原了主服务器和从服务器的my.cnf,一步一步来。

主服务器IP: 250.110.120.119
从服务器IP: 110.120.119.250

1.在主服务器新建一个用户,给从服务器同步使用。


GRANT ALL PRIVILEGES ON *.* TO 'tongbu'@'110.120.119.250.' IDENTIFIED BY 'tongbu';

意思是新建一个用户名为tongbu 密码为tongbu 具有所有操作权限的帐号。

2.修改主服务器my.cnf文件
其实这一步可以省略,因为保持默认的配置文件就可以了。
/*
添加
server-id=1
并开启log-bin二进制日志文件
log-bin=mysql-bin
注:需要把默认的server-id=1去掉
*/

3.重启主服务器mysql数据库

4.进入mysql界面执行下面命令 获取日志文件名和偏移量

show master status;

显示如:
mysql> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000010 | 106 | | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)

得到的日志文件名为 mysql-bin.000010 偏移量为106

5.修改从服务器的my.cnf
添加一个
server-id=2
注:需要把默认的server-id=1去掉

6.重启从服务器mysql数据库

7.对从数据库进行设置,@@@这是最最重要的地方。


change master to master_host='250.110.120.119', master_user='tongbu', master_password='tongbu', master_log_file='mysql-bin.000010', master_log_pos=106;

8.启动从服务器

start slave;

9.执行下面命令 查看进程

show processlist\G

显示如下
mysql> show processlist\G
*************************** 1. row ***************************
Id: 4
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 5
User: system user
Host:
db: NULL
Command: Connect
Time: 1773
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 6
User: system user
Host:
db: NULL
Command: Connect
Time: 1630
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
3 rows in set (0.00 sec)

10.查看从服务器状态

show slave status\G

显示如下
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 250.110.120.119
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 281
Relay_Log_File: up2-relay-bin.000002
Relay_Log_Pos: 426
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
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: 281
Relay_Log_Space: 579
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:
1 row in set (0.00 sec)

测试:
1、在主服务器test数据库中创建user表
mysql>use test;
mysql>create table user(id int);
2、在从服务器中查看user表
mysql>use test;
mysql> show tables like ‘user’;
+———————-+
| Tables_in_test(user) |
+———————-+
| user |
+———————-+
1 row in set (0.00 sec)
说明主从数据同步成功。

部分参考:http://database.51cto.com/art/201009/223213.htm

附录:


cp /usr/local/mysql/share/mysql/my-medium.cnf  /etc/my.cnf

/usr/local/mysql/bin/mysql -u root -p

show master status;

show variables like 'server_id';

set global server_id=2;

slave start;

slave stop;

show slave status\G

show processlist\G

flush privileges;

/usr/local/mysql/bin/mysql -h110.120.119.250 -uroot -proot123

############################################################

如果您看完文章,自己测试还是不成功,可以购买枫子的收费服务。

批量查询域名备案

请注明 mysql同步 50元部署主从实时同步。

  1. 还没有评论.想坐沙发?
  1. |
    2011年12月29日 at 23:20 | #1

    [...] 昨天搞定了数据库同步,今天理所应当就要搞定好文件同步。 [...]

  2. |
    2012年1月3日 at 03:58 | #2

    [...] Linux方面,基本的lnmpa一键配置环境,mysql优化,nginx优化,对国外的服务器市场行情比较了解,能够针对不同的需求选择不同的线路不同的机房购买服务器,能够简单的利用英文和老外交流了解决问题,mysql数据库主从同步,Linux利用rsync进行文件同步,对Linux中的shell编写有一点入门。 [...]

  3. |
    2012年1月7日 at 00:33 | #3

    [...] mysql的数据库的高可用性的架构大概有以下几种:集群,读写分离,主备。而后面两种都是通过复制来实现的。下面将简单介绍复制的原理及配置,以及一些常见的问题。mysql主从同步,请参考http://www.imfeng.com/linux-deploy-mysql-master-and-slave/ [...]

评论被关闭了