一台服务器启动两个mysql实例并且配置主从

背景:由于项目要求,在加上服务器资源有限,要尽快上线一个项目,因此直接搭建一套生产测试环境,供开发使用,其中涉及的到MySQL数据的主从配置,但是服务器只有一台。因此试着在搭建一台服务器,运行两个MySQL实例,且配置主从。
服务器系统版本:Centos6.3
MySQL数据库库版本:5.6.36
1、数据库的安装
卸载默认安装的MySQL数据库

rpm -qa | grep mysql
rpm -e --nodeps 对应的mysql版本

去官网下载MySQL安装包并且上传,然后解压安装

mkdir mysql && cd mysql
rz MySQL-5.6.36-1.el6.x86_64.rpm-bundle.tar
tart -xvf MySQL-5.6.36-1.el6.x86_64.rpm-bundle.tar
rpm -ivh MySQL*.rpm

2、修改配置文件、并且建立数据存储目录
首先查询MySQL的默认安装目录

#find / -name mysql -print
/var/lock/subsys/mysql
/var/lib/mysql          ##mysql库的默认安装路径
/var/lib/mysql/mysql
/usr/bin/mysql
/usr/lib64/perl5/DBD/mysql
/usr/lib64/perl5/auto/DBD/mysql
/usr/lib64/mysql
/usr/local/mysql
/usr/share/mysql        ##mysql的默认安装路径
/usr/include/mysql
/usr/include/mysql/mysql
/etc/logrotate.d/mysql
/etc/rc.d/init.d/mysql

建立数据库存储目录,添加属主属组

mkdir /mnt/data/{mysql-3306,mysql-3307} -pv
chown -R mysql:mysql /mnt/data

复制MySQL的主配置文件到/etc/目录下

cp -rf /usr/share/mysql/my-default.cnf /etc/my.cnf

编辑主配置文件

vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysql-cli]
default-character-set=utf8
[mysqld]
skip-name-resolve
max_connections=1000
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_name=1
max_allowed_packet=1024M
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
             
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
    
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = /var/lib/mysql  
 datadir = /mnt/data/mysql-3306
 port = 3388
 server_id = 1
 socket = /var/lib/mysql/mysql-3306.sock
 log-bin=mysql-bin
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

复制一份,并且重新命名

cp -rf /etc/my.cnf /etc/my3307.cnf

再次编辑,修改相关内容

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysql-cli]
default-character-set=utf8
[mysqld]
skip-name-resolve
max_connections=1000
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_name=1
max_allowed_packet=1024M
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = /var/lib/mysql  
 datadir = /mnt/data/mysql-3307
 port = 3307
 server_id = 2
 socket = /var/lib/mysql/mysql-3307.sock
 log-bin=mysql-bin
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

建库,并且赋予对应权限

mysql_install_db -datadir=/mnt/data/mysql-3306 --user=mysql
chmod -R 777 /mnt/data/mysql-3306 
mysql_install_db -datadir=/mnt/data/mysql-3307 --user=mysql
chmod -R 777 /mnt/data/mysql-3307 

启动端口为3306的MySQL数据库

service mysql start

启动端口为3307的MySQL数据库

/usr/bin/mysqld_safe --defaults-file=/etc/my3307.cnf &

检查端口

#ss -tnl
Recv-Q Send-Q                        Local Address:Port                                         Peer Address:Port 
0      128                                 :::3306                                                  :::*   
0      128                                 :::3307                                                   :::*

3、配置数据库的主从服务
登录3306数据库

mysql -uroot -p -S /var/lib/mysql/mysql-3306.sock

更改root默认密码

set password for 'root'@'localhost' =password('123456');

授权root远程登录

grant all privileges on *.* to root@'%' identified by '123456' with grant option;
flush privileges;

登录3307数据库

mysql -uroot -p -S /var/lib/mysql/mysql-3307.sock

更改root默认密码

set password for 'root'@'localhost' =password('123456');

授权root远程登录

grant all privileges on *.* to root@'%' identified by '123456' with grant option;
flush privileges;

4、配置主从复制
在主服务器上建立帐户并授权slave:

mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';
登录主服务器的mysql,查询master的状态
   mysql>show master status;
   +------------------+----------+--------------+------------------+
   | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
   +------------------+----------+--------------+------------------+
   | mysql-bin.000002 |      399 |              |                  |
   +------------------+----------+--------------+------------------+
   1 row in set (0.00 sec)
   注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化

配置从服务器Slave:

 mysql>change master to master_host='localhost',master_user='mysync',master_password='q123456',master_port='3306',master_log_file='mysql-bin.000002',master_log_pos=399;   //注意不要断开,399数字前后无单引号。

 mysql>start slave;    //启动从服务器复制功能

检查从服务器复制功能状态:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: localhost
                  Master_User: mysync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 399
               Relay_Log_File: MasService-relay-bin.000002
                Relay_Log_Pos: 399
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...................................................
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。

5、主从服务器测试:

主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
 mysql> create database hi_db;
  Query OK, 1 row affected (0.00 sec)

  mysql> use hi_db;
  Database changed

  mysql>  create table hi_tb(id int(3),name char(10));
  Query OK, 0 rows affected (0.00 sec)
 
  mysql> insert into hi_tb values(001,'bobu');
  Query OK, 1 row affected (0.00 sec)

  mysql> show databases;
   +--------------------+
   | Database           |
   +--------------------+
   | information_schema |
   | hi_db                |
   | mysql                |
   | test                 |
   +--------------------+
   4 rows in set (0.00 sec)

从服务器Mysql查询:

   mysql> show databases;

   +--------------------+
   | Database               |
   +--------------------+
   | information_schema |
   | hi_db                 |       //I'M here,大家看到了吧
   | mysql                 |
   | test          |
   +--------------------+
   4 rows in set (0.00 sec)

   mysql> use hi_db
   Database changed
   mysql> select * from hi_tb;           //查看主服务器上新增的具体数据
   +------+------+
   | id   | name |
   +------+------+
   |    1 | bobu |
   +------+------+
   1 row in set (0.00 sec)

6、从服务器配置只读属性

从服务器“read_only=1”只读模式开启的解锁命令为设定“read_only=0”;设定全局锁“flush tables with read lock;”,对应的解锁模式命令为:“unlock tables;”

 set global read_only=1;####给从服务器设置只读
 set global read_only=0;####取消设置只读属性

7、完成:

    编写一shell脚本,用nagios监控slave的两个yes(Slave_IO及Slave_SQL进程),如发现只有一个或零个yes,就表明主从有问题了,发短信警报吧。
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,376评论 6 491
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,126评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,966评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,432评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,519评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,792评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,933评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,701评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,143评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,488评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,626评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,292评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,896评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,742评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,977评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,324评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,494评论 2 348

推荐阅读更多精彩内容