您的位置:首页 > 数据库 > MySQL

mysql主主-配置数据同步

2011-03-29 13:58 726 查看
一、环境

192.168.18.48 mysql1

192.168.18.49 mysql2

二,安装配置

1.安装mysql

tar zxf mysql-5.0.37.tar.gz

groupadd mysql ; useradd -g mysql mysql

chown -R mysql.mysql /usr/local/mysql/

mkdir /usr/local/mysql

chown -R mysql.mysql /usr/local/mysql/

cd mysql-5.0.37

./configure --prefix=/usr/local/mysql/ --with-mysqld-user=mysql --with-charset=utf8 --with-extra-charsets=all --enable-thread-safe-client --with-innodb --with-unix-socket-path=/tmp/mysql.sock

make && make install

cp support-files/my-innodb-heavy-4G.cnf /etc/my.cnf

cd scripts/

./mysql_install_db

chown -R mysql.mysql /usr/local/mysql/

chown -R mysql.mysql /usr/local/mysql/var/

cd /usr/local/mysql/share/

./mysql/mysql.server start

/bin/cp -rp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld

chmod 755 /etc/init.d/mysqld

cd /etc/init.d/

chkconfig --add mysqld

chkconfig --level 345 mysqld on

chkconfig --level 0126 mysqld off

service mysqld restart

2.配置mysql

1) 在两台机器上给对方授权

GRANT REPLICATION SLAVE ON *.* to 'backup'@'192.168.%' IDENTIFIED BY '123456';

flush privileges;

2)配置主配置文件

在mysql1上

[mysqld]

server-id = 10

log-bin = mysql-bin

log-slave-updates

auto-increment-increment = 2

auto-increment-offset = 1

replicate-do-db = mysqldb

在mysql2上

[mysqld]

server-id = 20

log-bin = mysql-bin

log-slave-updates

auto-increment-increment = 2

auto-increment-offset = 2

replicate-do-db = mysqldb

注:二库都只有server-id不同和 auto-increment- offset不同

auto-increment-offset是用来设定数据库中自动增长的起点的,回为这两能服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突

replicate-do-db 指定同步的数据库,我们只在两台服务器间同步mydb数据库

另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2

(这段标注是抄袭51cto好友“红豆杀”的其主页是http://home.51cto.com/index.php?s=/space/2517622 ^_^ 在他这学到不少东西。)

#配置完成后,重启两台数据库

service mysqld restart

3)同步两台数据库

首先先在两台数据库上创建要同步的数据库

create database mysqldb;

分别在两台数据库中,执行下命令,查看potion,以及binlog。(因为我的两台数据库都是新安装的,所以binlog以及potion都一样,如果是使用已有的数据库,首先需要把两台数据库同步,可以使用mysqldump工具。然后查看binlog,以及potion的位置。)

mysql> show master status;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000004 | 324 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

##在 mysql1 上

stop slave;

CHANGE MASTER TO master_host='192.168.18.48', master_user='backup', master_password='123456', MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=324;

stop slave;

##在 mysql2 上

stop slave;

CHANGE MASTER TO master_host='192.168.18.49', master_user='backup', master_password='123456', MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=324;

stop slave;

##分别查看两台数据库的slave状态。

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

##都为yes就ok了

4)验证mysql主主

###好了现在我们来验证吧。。。

在mysql1的mysqldb数据库中创建表

create table haifengtest ( name varchar(15) not null, city varchar(15) not null, birth date not null);

在mysql2的mysqldb数据库中能够查看到该表

mysql> show tables;

+-------------------+

| Tables_in_mysqldb |

+-------------------+

| haifengtest |

+-------------------+

1 row in set (0.00 sec)

在mysql2中向表haifengtest插入数据

insert into haifengtest values ("haifeng","beijing","1986-**-**");

在mysql1中查看表haifengtest

mysql> select * from haifengtest;

+---------+---------+------------+

| name | city | birth |

+---------+---------+------------+

| haifeng | beijing | 1986-**-** |

+---------+---------+------------+

1 row in set (0.00 sec)

###好了 第一步 mysql主主大公告成。。。。
本文出自 “海风的linux之路” 博客,请务必保留此出处http://lhflinux.blog.51cto.com/1961662/529772
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: