MySQL MHA配置详解

vivenwan 2015-02-10

    MySQL环境:

    master:192.168.202.129:3306

    slave:192.168.202.129:3307,192.168.202.129:3307,192.168.202.130:3306,192.168.202.130:3307,192.168.202.130:3308

    MariaDB-10.0.13

    MHA环境:

    manager:192.168.202.129

    node:192.168.202.130(3306端口监听的实例作为备用主库),192.168.202.129

    下载地址:http://code.google.com/p/mysql-master-ha/ (这个需要FQ),

也可以在安科网的资源站里下载:

------------------------------------------分割线------------------------------------------

具体下载目录在 /2015年资料/2月/10日/MySQL MHA配置详解/

------------------------------------------分割线------------------------------------------

    一 MySQL安装准备:

    1 建立MySQL安装目录:mkdir -p /usr/local/mysql

    2 建立mysql用户:

   

groupadd mysql
useradd -g mysql mysql

    3 下载依赖包:

   

yum  install make apr* autoconf automake curl curl-devel gcc gcc-c++ gtk+-devel zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat*  cpp glibc libgomp libstdc++-devel keyutils-libs-devel libsepol-devel libselinux-devel krb5-devel  libXpm* freetype freetype-devel freetype* fontconfig fontconfig-devel  libjpeg* libpng* php-common php-gd gettext gettext-devel ncurses* libtool* libxml2 libxml2-devel patch policycoreutils bison

    4 下载cmake:yum install cmake -y

    5 开始编译:

   

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/home/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1

    6 结束之后,输入下面的命令:make && make install

    上面的过程结束之后就安装好了MySQL。下面是配置过程:

  二 编译安装MySQL:

  1 修改环境变量:vi /etc/profile,添加下面的语句:

 

export PATH=$PATH:/usr/local/mysql/bin

  2 source /etc/profile加载环境变量

  3 将/usr/local/mysql/support_files下的my-default.cnf复制到/etc下:cp /usr/local/mysql/support_files/my-default.cnf /etc/my.cnf,修改这个文件加入下面的内容:

 

 

[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /home/mysql/mydata/log/mysqld_multi.log


[mysqld1]
socket = /home/mysql/mydata/data1/socket/mysqld.sock
port = 3306
pid-file = /home/mysql/mydata/data1/mysqld.pid
datadir = /home/mysql/mydata/data1

log_bin=/home/mysql/mydata/data1/binlog/mysql-bin
server-id = 1

[mysqld2]
socket = /home/mysql/mydata/data2/socket/mysqld.sock
port = 3307
pid-file = /home/mysql/mydata/data2/mysqld.pid
datadir = /home/mysql/mydata/data2

log_bin=/home/mysql/mydata/data2/binlog/mysql-bin
server-id = 2
relay_log = /home/mysql/mydata/data2/relay_log/mysql-relay-bin
log_slave_updates = 1
read_only = 1

[mysqld3]
socket = /home/mysql/mydata/data3/socket/mysqld.sock
port = 3308
pid-file = /home/mysql/mydata/data3/mysqld.pid
datadir = /home/mysql/mydata/data3

log_bin=/home/mysql/mydata/data3/binlog/mysql-bin
server-id = 3
relay_log = /home/mysql/mydata/data3/relay_log/mysql-relay-bin
log_slave_updates = 1
read_only = 1

 

    上面的路径都需要用mysql用户建立,这里不再赘述。

    4 下面就可以初始化数据库了:

   

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/mydata/data1

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/mydata/data2

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/mydata/data3

    这样就把一个服务器上所有的数据库都建立好了,在130服务器上也是如法炮制,不过在修改my.cnf的时候要注意一点就是修改server-id,从4以后开始,不要和129服务器的相同。

    下面就可以启动数据库了,命令很简单:

   

mysqld_multi --defaults-extra-file=/etc/my.cnf start 1,2,3

    这就将三个数据库启动好了,可以用下面的语句检查:

   

mysqld_multi --defaults-extra-file=/etc/my.cnf report

MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld3 is running

   

    三 主从配置

    1 主库建立复制用户:

   

在192.168.202.129上:
$mysql -uroot -P3306 -p127.0.0.1

grant replication slave, replication client on *.* to repl@'192.168.202.%' identified by 'repl'; -- 这里限制在局域网内


    2 从库配置:

   

 

在192.168.202.129上:
$mysql -uroot -P3307 -p127.0.0.1
change master to master_host='192.168.202.129', MASTER_PORT=3306, master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=0;
start slave;


$mysql -uroot -P3308 -p127.0.0.1
change master to master_host='192.168.202.129', MASTER_PORT=3306, master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=0;
start slave;
在192.168.202.130上:
$mysql -uroot -P3306 -p127.0.0.1
change master to master_host='192.168.202.129', MASTER_PORT=3306, master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=0;
start slave;
$mysql -uroot -P3307 -p127.0.0.1
change master to master_host='192.168.202.129', MASTER_PORT=3306, master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=0;
start slave;
$mysql -uroot -P3308 -p127.0.0.1
change master to master_host='192.168.202.129', MASTER_PORT=3306, master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=0;
start slave;

 

    这样子就配置结束了。

    3 配置好了以后主库上会有slave过来的进程:

    mysql>show processlist;

MySQL MHA配置详解

    有几个slave,这里就会有多少个进程。

    4

# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

相关推荐