码迷,mamicode.com
首页 > 数据库 > 详细

搭建mysql主主

时间:2015-11-23 19:21:05      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:搭建mysql主主

搭建mysql主主

技术分享

实验环境 192.168.137.11 (lamp) 和192.168.137.12(mysql)

先搭建主从,在搭建主主。

1.搭建环境 根据auto_lamp.tar 里面的安装包和脚本来搭建。

192.168.137.11  运行脚本auto_lamp_3.0.sh

技术分享

选择7自动安装lamp平台。

 

192.168.137.11  运行脚本auto_lamp_3.0.sh

技术分享

选择4自动安装mysql平台。

2.配置mysql主从。

a.    在192.168.137.11上。

[root@hx~]# vim /etc/my.cnf

[client]

port            = 3306

socket          = /tmp/mysql.sock

[mysqld]

port            = 3306

socket          = /tmp/mysql.sock

skip-external-locking

key_buffer_size= 384M

max_allowed_packet= 1M

table_open_cache= 512

sort_buffer_size= 2M

read_buffer_size= 2M

read_rnd_buffer_size= 8M

myisam_sort_buffer_size= 64M

thread_cache_size= 8

query_cache_size= 32M

thread_concurrency= 8

datadir=/mydata/data/

log-bin=mysql-bin

server-id       = 1

auto_increment_offset=1

auto_increment_increment=2

[mysqldump]

quick

max_allowed_packet= 16M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size= 256M

sort_buffer_size= 256M

read_buffer= 2M

write_buffer= 2M

[mysqlhotcopy]

interactive-timeout

[mysql_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

replicate-do-db=all

 

***背景的参数为自己添加的。

这里有几个重要的参数。

log-bin=mysql-bin     #开启bin-log日志,记录msql主上的操作。

server-id       = 1   #mysql服务id

auto_increment_offset=1

auto_increment_increment=2

(mysql中有自增长字段,在做数据库的主主同步时需要设置自增长的两个相关配置:

auto_increment_offset和auto_increment_increment。

auto_increment_offset表示自增长字段从那个数开始,他的取值范围是1-65535

auto_increment_increment表示自增长字段每次递增的量,其默认值是1,取值范围是1-65535

在主主同步配置时,需要将两台服务器的auto_increment_increment增长量都配置为2,而要把auto_increment_offset分别配置为1和2,这样才可以避免两台服务器同时做更新时自增长字段的值之间发生冲突。)

replicate-do-db=all      #允许slave同步哪个库

 

重启mysqld服务

[root@hx~]# /etc/init.d/mysqld restart

Shuttingdown MySQL. SUCCESS!

StartingMySQL.. SUCCE

 

b.    在192.168.137.12上

       [root@localhost ~]# vim /etc/my.cnf

[client]

port            = 3306

socket          = /tmp/mysql.sock

[mysqld]

port            = 3306

socket          = /tmp/mysql.sock

skip-external-locking

key_buffer_size= 384M

max_allowed_packet= 1M

table_open_cache= 512

sort_buffer_size= 2M

read_buffer_size= 2M

read_rnd_buffer_size= 8M

myisam_sort_buffer_size= 64M

thread_cache_size= 8

query_cache_size= 32M

thread_concurrency= 8

datadir=/mydata/data/

log-bin=mysql-bin

server-id       = 2

auto_increment_offset=2

auto_increment_increment=2

[mysqldump]

quick

max_allowed_packet= 16M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size= 256M

sort_buffer_size= 256M

read_buffer= 2M

write_buffer= 2M

[mysqlhotcopy]

interactive-timeout

[mysql_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

replicate-do-db=all

 

修改了server-id=2 还有auto_increment_increment=2

其他没变化,然后重启下mysql服务。

 

c.     搭建mysql主从

在192.168.137.11上操作

[root@hx ~]# /usr/local/mysql/bin/mysql     #进入数据库

mysql> grant replication slave  on *.* to tongbu@‘192.168.137.12‘ identifiedby ‘123456‘;

Query OK, 0 rows affected (0.01 sec)

mysql> show master status ;

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

| File             | Position | Binlog_Do_DB |Binlog_Ignore_DB |

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

| mysql-bin.000004 |      268 |              |                  |

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

1 row in set (0.01 sec)

 

在192.168.137.12mysql从上操作

[root@hx ~]# /usr/local/mysql/bin/mysql     #进入数据库

mysql> change master tomaster_host=‘192.168.137.11‘,master_user=‘tongbu‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000004‘,master_log_pos=268;

Query OK, 0 rows affected (0.03 sec)

mysql> start slave ;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G

*************************** 1. row***************************

              Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.137.11

                  Master_User: tongbu

                  Master_Port: 3306

                Connect_Retry: 60

             Master_Log_File: mysql-bin.000004

         Read_Master_Log_Pos: 268

              Relay_Log_File: localhost-relay-bin.000002

                Relay_Log_Pos: 253

       Relay_Master_Log_File: mysql-bin.000004

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

                     .....省略

只要看到io线程和sql线程都是yes代表,mysql主从搭建完毕。

测试mysql主从。

在mysql主上创建数据库(192.168.137.11)

mysql> create database bbs ;

Query OK, 1 row affected (0.00 sec)

mysql> show databases ;

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

| Database           |

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

| information_schema |

|bbs                |

| mysql              |

| performance_schema |

| test               |

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

5 rows in set (0.00 sec)

在mysql从上查看是否创建bbs数据库。(192.168.137.12)

mysql> show databases;

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

| Database           |

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

| information_schema |

|bbs                |

| mysql              |

| performance_schema |

| test               |

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

5 rows in set (0.00 sec)

测试完成,mysql主从已经搭建完成,下面开始搭建mysql主主。

 

3.    配置mysql主主

a.    首先删除mysql主下面的,mysql-bin.index 和下面的日志

[root@hx ~]# cd /mydata/data/

[root@hx data]# ls

bbs    ibdata1      mysql             mysql-bin.000003  performance_schema

hx.err ib_logfile0  mysql-bin.000001  mysql-bin.000004  test

hx.pid ib_logfile1  mysql-bin.000002  mysql-bin.index

[root@hx data]# rm -rf mysql-bin.*

b.    在mysql主上授权(192.168.137.12)

[root@localhost ~]#/usr/local/mysql/bin/mysql   #进去mysql数据库

mysql> grant replication slave on *.* totongbu@‘192.168.137.11‘ identified by ‘123456‘;

Query OK, 0 rows affected (0.00 sec)

mysql> show master status ;

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

| File             | Position | Binlog_Do_DB |Binlog_Ignore_DB |

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

| mysql-bin.000004 |      267 |              |                  |

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

1 row in set (0.01 sec)

c.     在mysql从上设置节点,和主ip

[root@hx data]# /usr/local/mysql/bin/mysql

mysql> change master tomaster_host=‘192.168.137.12‘,master_user=‘tongbu‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000004‘,master_log_pos=267;

Query OK, 0 rows affected (0.01 sec)

 

mysql> slave start;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G

*************************** 1. row***************************

              Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.137.12

                  Master_User: tongbu

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000004

         Read_Master_Log_Pos: 267

              Relay_Log_File: hx-relay-bin.000002

                Relay_Log_Pos: 253

       Relay_Master_Log_File: mysql-bin.000004

            Slave_IO_Running: Yes

           Slave_SQL_Running: Yes

              ....省略

另一方的mysql从上也搭建好了,我们可以来测试mysql主主了。

d.    测试mysql主主

在192.168.137.11 和 192.168.137.12 上分别创建bbs1 和 bbs2

技术分享

技术分享

测试成功,mysql主主搭建完毕。看下图..

技术分享

技术分享



本文出自 “success” 博客,请务必保留此出处http://maxiaotian.blog.51cto.com/10824115/1715999

搭建mysql主主

标签:搭建mysql主主

原文地址:http://maxiaotian.blog.51cto.com/10824115/1715999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!