mysql 主从复制 && 失败切换

软件环境:

mysql软件包:mysql-5.6.17.tar.gz

cmake软件包:cmake-2.8.12.2.tar.gz @  wget http://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz

系统版本:

[[email protected] src]# uname -a
Linux db.jrzj.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

[[email protected] yum.repos.d]# uname -a
Linux backup.jrzj.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

开始安装:

==============

1:安装cmake:

[[email protected] cmake-2.8.12.2]# yum install gcc
[[email protected] cmake-2.8.12.2]# yum install gcc-C++
[[email protected] cmake-2.8.12.2]# yum install make
[[email protected] cmake-2.8.12.2]# ./configure 
[[email protected] cmake-2.8.12.2]# make && make install

2:安装mysql

为mysql创建用户

[[email protected] mysql-5.6.17]# useradd mysql

创建mysql数据文件存放目录

[[email protected] mysql-5.6.17]# mkdir /data/

开始安装mysql

[[email protected] mysql-5.6.17]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/ -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=GBK -DDEFAULT_COLLATION=gbk_chinese_ci

[[email protected] mysql-5.6.17]# make && make install

error for cmake

CMake Error at cmake/readline.cmake:85 (MESSAGE):

Curses library not found.  Please install appropriate package,

remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.

Call Stack (most recent call first):

cmake/readline.cmake:128 (FIND_CURSES)

cmake/readline.cmake:202 (MYSQL_USE_BUNDLED_EDITLINE)

CMakeLists.txt:411 (MYSQL_CHECK_EDITLINE)

[[email protected] mysql-5.6.17]# yum install ncurses-devel

[[email protected] mysql-5.6.17]# rm CMakeCache.txt

rm:是否删除普通文件 "CMakeCache.txt"?y

初始化mysql

[[email protected] /]#  chown -R mysql.mysql /usr/local/mysql 
[[email protected] /]#  chown -R mysql.mysql /data/
[[email protected] /]#  cd /usr/local/mysql/scripts
[[email protected] scripts]#  ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/ --collation-server=gbk_chinese_ci

[[email protected] scripts]# cd ../support-files/

[[email protected] support-files]# cp mysql.server /etc/rc.d/init.d/mysqld

[[email protected] support-files]# chkconfig --add mysqld

[[email protected] support-files]# chkconfig mysqld on

[[email protected] support-files]# echo ‘PATH=$PATH:/usr/local/mysql/bin/‘ >> /etc/profile

[[email protected] support-files]# source /etc/profile

[[email protected] support-files]# mysql_secure_installation

[[email protected] support-files]# service  mysqld start

Starting MySQL. SUCCESS!

[[email protected] support-files]# mysqladmin -u root password 1234qwer

[[email protected] support-files]# mysql -u root -p

Enter password:

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> status

--------------

mysql  Ver 14.14 Distrib 5.6.17, for Linux (x86_64) using  EditLine wrapper

Connection id: 7

Current database:

Current user: [email protected]

SSL: Not in use

Current pager: stdout

Using outfile: ‘‘

Using delimiter: ;

Server version: 5.6.17 Source distribution

Protocol version: 10

Connection: Localhost via UNIX socket

Server characterset: gbk

Db     characterset: gbk

Client characterset: gbk

Conn.  characterset: gbk

UNIX socket: /tmp/mysqld.sock

Uptime: 4 min 33 sec

Threads: 1  Questions: 19  Slow queries: 0  Opens: 67  Flush tables: 1  Open tables: 60  Queries per second avg: 0.069

--------------

============

单节点mysql安装完成

============

重复以上操作安装mysql_backup

=============

设置root用户可远程登录

grant all privileges  on *.* to [email protected]‘%‘ identified by "root";
mysql -h 192.168.0.176 -u root -p

============

设置主从:

主DB:

[[email protected] etc]# mysql -u root -p
Enter password: 
mysql>  create user jrzj IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* to ‘jrzj‘@‘192.168.0.176‘ identified by ‘1234qwer‘;
Query OK, 0 rows affected (0.00 sec)
[[email protected] etc]# vim /etc/my.cnf
server_id=177
log-bin=jrzj-bin 
[[email protected] etc]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

查看日志偏移量:

mysql> show master status \G

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

File: jrzj-bin.000002

Position: 120

Binlog_Do_DB:

Binlog_Ignore_DB:

Executed_Gtid_Set:

1 row in set (0.00 sec)

mysql> flush tables with read lock;

导出数据库:

[[email protected] tmp]# mysqldump  -p3306 -uroot -p  -S /tmp/mysqld.sock  --all-databases > /tmp/mysql.sql

从DB配置

[[email protected] etc]# vim /etc/my.cnf
server_id=176

[[email protected] tmp]# mysql -uroot -p < /tmp/mysql.sql 
mysql> change master to
    -> master_host=‘192.168.0.177‘,
    -> master_user=‘jrzj‘,
    -> master_password=‘1234qwer‘,
    -> master_port=3306,
    -> master_log_file=‘jrzj-bin.000002‘,
    -> master_log_pos=120;
mysql>  show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.177
                  Master_User: jrzj
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: jrzj-bin.000002
          Read_Master_Log_Pos: 120
               Relay_Log_File: backup-relay-bin.000002
                Relay_Log_Pos: 282
        Relay_Master_Log_File: jrzj-bin.000002
             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: 120
              Relay_Log_Space: 456
              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: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 177
                  Master_UUID: e0fe5f94-c633-11e3-8f4e-0050568ea576
             Master_Info_File: /data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0

=============

主从配置完成

==========

将mysql配置为互为主从,开启slave的bin log 并授权访问

=========

高可用配置

=========

0.176/0.177

[[email protected] tmp]# yum install ipvsadm keepalived
[[email protected] tmp]# chkconfig keepalived on

[[email protected] tmp]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server smtp.126.com

smtp_connect_timeout 30

router_id HA

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

priority 100

advert_int 1

preempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.16

}

}

[[email protected] tmp]# cat /tmp/check_mysql.sh

MYSQL="/usr/local/mysql/bin/mysql"

MYSQL_HOST=127.0.0.1

MYSQL_USER=root

MYSQL_PASSWORD=1234qwer

LOG_FILE="/tmp/log/check_mysql.log"

MYSQL_OK=1 # mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0

check_mysql_helth()

{

$MYSQL -h$MYSQL_HOST -u$MYSQL_USER -p${MYSQL_PASSWORD} -e "show status" &>/dev/null

if [ $? = 0 ] ; then

MYSQL_OK=1

else

MYSQL_OK=0

fi

return $MYSQL_OK

}

check_keepalived()

{

ps -ef | grep keepalived | grep -v grep > /dev/null

if [ $? = 0 ];then

keepalived_OK=1

else

keepalived_OK=0

fi

return $keepalived_OK

}

while :

do

CHECK_TIME=3

while [ $CHECK_TIME -ne 0 ]

do

let "CHECK_TIME -= 1"

check_mysql_helth

if [ $MYSQL_OK == 1 ];then

check_keepalived

if [ $keepalived_OK == 1 ];then

break

else

service keepalived start >>$LOG_FILE

break

fi

elif [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ];then

service keepalived stop >> $LOG_FILE

echo `date --date=today +"%Y-%M-%d %H:%m:%S"` - [INFO] - mysql invaild. keepalived stop. >> $LOG_FILE

fi

done

done

[[email protected] tmp]#service mysqld start

[[email protected] tmp]#service keepalibed start

[[email protected] tmp]# nohup ./check_mysql.sh &

mysql 主从复制 && 失败切换

时间: 2024-10-01 20:25:50

mysql 主从复制 && 失败切换的相关文章

mysql主从复制失败(uuid)

我这里用docker镜像生成的两个容器,所以会导致主从复制失败:主从的uuid(UUID 是 通用唯一识别码)相同 . mysql> stop slave; Query OK, 0 rows affected (0.01 sec) mysql> CHANGE MASTER TO MASTER_HOST='172.18.0.201', -> MASTER_PORT=3306, -> MASTER_USER='rep', -> MASTER_PASSWORD='123', -&g

Mysql 基于GTID的主从复制及切换

参考 http://imysql.com/tag/gtid http://mysqllover.com/?p=594 Mysql 基于GTID的主从复制及切换 一.主从复制配置 两个mysql服务的my.cnf 中相关内容配置 [mysqld] #从复制数据库表设置 replicate-wild-ignore-table = mysql.%,information_schema.%,innodb.%,innodb_log.%,performance_schema.%,test.%,tmp.% #

mysql主从复制配置操作以及主从宕机切换演练

主从复制目的: 主从服务器设置的稳健性得以提升,如果主服务器发生故障,可以把本来作为备份的从服务器提升为新的主服务器.在主从服务器上分开处理用户的请求,读的话,可以直接读取备机数据,可获得更短的响应时间. 主服务器:IP地址192.168.80.129,mysql已经安装,无用户数据. 从服务器:IP地址192.168.80.130,mysql已经安装. 注:数据库版本必须一致. 1.主从复制配置 修改从服务器的配置文件/etc/my.cnf,在mysqld里添加一下属性 [mysqld] lo

keepalived实现对mysql主从复制的主备自动切换

备注:君子性非议也,善假于物也. 温故而知新,可以为师矣. 使用MySQL+keepalived是一种非常好的解决方案,在MySQL-HA环境中,MySQL互为主从关系,这样就保证了两台 MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换. 实验环境中用两台主机搭建了一个mysql主从复制的环境,两台机器分别安装了keepalived,用一个虚IP实现mysql服务器的主备自动切换功能. 模拟环境: VIP:1

MySQL主从复制介绍

1.1 MySQL主从复制原理介绍 MySQL的主从复制是一个异步的复制过程(虽然一般情况下感觉是实时的),数据将从一个MySQL数据库(我们称之为Master)复制到另一个MySQL数据库(我们称之为Slave),在Master与Slave之间实现整个主从复制的过程是由三个线程参与完成的,其中有两个线程(SQL线程和IO线程)在Slave端,另外一个线程(I/O线程)在Master端. 要实现MySQL的主从复制,首先必须打开Master端的binlog记录功能,否则就无法实现.因为整个复制过

MySQL主从复制(二)

主从架构中:从node是不接受w操作的,否则可能会导致数据不一致. 一.复制架构中应该注意的问题: 1.限制slave为只读模式 可以设置在启动参数中. > show global variables like 'read_only'; 此限制对拥有SUPER权限的用户都无效. 阻止所有用户: mysql> flush tables with read lock; //将阻塞所有w操作. //但是中继日志的重放是可以的,不会被阻塞 2.如何保证主从复制的事务安全? master在执行事务后,应

MySQL主从复制实现数据库服务器双机热备详细讲解

1.mysq主从复制简介 1.1.复制介绍 MySQL支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环.这些日志可以记录发送到从服务器的更新.当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置.从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新.如果你想要设置链式复制服务器,从服务器本身也可以充当主服务器.请注意当你进行复制时,所

MySQL主从复制的常见拓扑、原理分析以及如何提高主从复制的效率总结

一.主从复制搭建方法参考 1.MySQL5.6 数据库主从(Master/Slave)同步安装与配置详解请参考: http://blog.csdn.net/xlgen157387/article/details/51331244#comments 2.使用mysqlreplicate命令快速搭建 Mysql 主从复制: http://blog.csdn.net/xlgen157387/article/details/52452394 二.Mysql 主从复制的常用拓扑结构 2.1.一主一从 是最

第五章:MySQL主从复制

目录 1. 主从MySQL主从复制介绍... 1 2. MySQL主从复制的企业应用场景... 3 3. 实现MySQL主从读写分离的方案... 5 4. MySQL主从复制原理... 7 5. 主从复制实战... 8 5.1 MySQL主从配置... 8 5.2 MySQL数据库级联配置... 12 5.3 MySQL主从复制配置步骤小结... 15 5.4 主从配置表示成功后的关键参数说明... 15 5.5 MySQL主从复制配置问题汇总... 16 6. MySQL主从复制更多应用技巧.