说明:
今天接着研究DRBD的第二个应用,利用DRBD+Heartbeat+Mysql:配置mysql的高可用
环境:
[[email protected] ~]# cat /etc/issue CentOS release 6.4 (Final) Kernel \r on an \m [[email protected] ~]# uname -r 2.6.32-358.el6.i686
dbm137 | 192.168.186.137 | dbm137.51.com | primary | DRBD+Heartbeat+Mysql |
dbm138 | 192.168.186.138 | dbm138.51.com | secondary | DRBD+Heartbeat+Mysql |
vip | 192.168.186.200 |
准备工作和安装DRBD:
http://732233048.blog.51cto.com/9323668/1665979
安装配置mysql:
安装mysql:(dbm137,dbm138)
##安装依赖包: [[email protected] ~]# yum -y install gcc gcc-c++ autoconf automake bison ncurses-devel libtool-ltdl-devel* cmake make
[[email protected] ~]# mkdir -p /opt/mysql/data #创建数据库的临时数据目录 [[email protected] ~]# cd /usr/local/src/ [[email protected] src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.22.tar.gz [[email protected] src]# tar -zxf mysql-5.6.22.tar.gz [[email protected] src]# cd mysql-5.6.22 [[email protected] mysql-5.6.22]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/opt/mysql/data -DSYSCONFDIR=/usr/local/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS:STRING=utf8,gbk -DWITH_DEBUG=0 ##注意-DCMAKE_INSTALL_PREFIX,-DMYSQL_DATADIR和-DSYSCONFDIR选项的值 [[email protected] mysql-5.6.22]# make #这个过程可能会很久 [[email protected] mysql-5.6.22]# make install [[email protected] ~]# groupadd mysql #创建mysql用户组 [[email protected] ~]# useradd -s /sbin/nologin -g mysql mysql [[email protected] ~]# chown -R mysql.mysql /opt/mysql #授权数据目录 [[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/opt/mysql/data --defaults-file=/usr/local/mysql/my.cnf --user=mysql #初始化数据库
配置mysql:
dbm137(Primary)
[[email protected] ~]# mount /dev/drbd0 /data/ #配置前先把DRBD设备挂载 [[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 19G 3.4G 14G 20% / tmpfs 58M 0 58M 0% /dev/shm /dev/sda1 477M 43M 409M 10% /boot /dev/drbd0 8.8G 21M 8.3G 1% /data [[email protected] ~]# mkdir -p /data/mysql/binlog #创建二进制日志目录 [[email protected] ~]# mkdir -p /data/mysql/data #创建数据库的数据目录 [[email protected] ~]# cp -a /opt/mysql/data/* /data/mysql/data/ #拷贝数据到数据库目录 [[email protected] ~]# chown -R mysql.mysql /data/mysql/ #授权mysql目录 [[email protected] ~]# cd /data/mysql/data/ [[email protected] data]# ll total 110604 -rw-rw---- 1 mysql mysql 12582912 Jul 2 12:00 ibdata1 -rw-rw---- 1 mysql mysql 50331648 Jul 2 12:00 ib_logfile0 -rw-rw---- 1 mysql mysql 50331648 Jul 2 12:00 ib_logfile1 drwx------ 2 mysql mysql 4096 Jul 2 12:00 mysql drwx------ 2 mysql mysql 4096 Jul 2 12:00 performance_schema drwx------ 2 mysql mysql 4096 Jul 2 12:00 test [[email protected] ~]# mv /usr/local/mysql/my.cnf /usr/local/mysql/my.cnf.old [[email protected] ~]# vi /usr/local/mysql/my.cnf #修改配置文件 [mysqld] basedir = /usr/local/mysql #安装路径 datadir = /data/mysql/data #数据目录 port = 3306 server_id = 1 #此值不要去修改 pid-file = /data/mysql/data/mysql.pid #进程文件 socket = /var/lib/mysql/mysql.sock default_storage_engine = InnoDB log-bin = /data/mysql/binlog/mysql-binlog #二进制文件 expire_logs_days = 14 max_binlog_size = 5G binlog_cache_size = 10M max_binlog_cache_size = 20M slow_query_log long_query_time = 2 slow_query_log_file = /data/mysql/data/slow.log #slow日志文件 open_files_limit = 65535 innodb = FORCE innodb_buffer_pool_size = 100M #注意这个参数 innodb_log_file_size = 1G query_cache_size = 0 thread_cache_size = 64 table_definition_cache = 512 table_open_cache = 512 max_connections = 20 sort_buffer_size = 10M max_allowed_packet = 6M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [client] #socket = /var/lib/mysql/mysql.sock ##注意:innodb_buffer_pool_size: ##主要作用是缓存innodb表的索引,数据,插入数据时的缓冲 ##默认值:128M ##专用mysql服务器设置此值的大小:系统内存的70%-80%最佳 ##如果你的系统内存不大,查看这个参数,把它的值设置小一点吧(若值设置大了,启动会报错)
[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #拷贝启动脚本 [[email protected] ~]# /etc/init.d/mysqld start #启动mysql服务 Starting MySQL.................................... SUCCESS! ##注意: ##千万不要,千万不要设置mysql开机自动启动,因为mysql服务的启动由heartbeat统一管理 [[email protected] ~]# ps -ef | grep mysql root 16284 1 0 12:46 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql.pid mysql 16783 16284 6 12:46 pts/1 00:00:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --open-files-limit=65535 --pid-file=/data/mysql/data/mysql.pid --socket=/var/lib/mysql/mysql.sock --port=3306 root 16878 1286 0 12:47 pts/1 00:00:00 grep mysql [[email protected] ~]# vi /etc/profile #修改PATH路径 ##在最后添加: export PATH=$PATH:/usr/local/mysql/bin [[email protected] ~]# source /etc/profile #立即生效
dbm138(secondary)
##下面的操作一定要看仔细 ##先进行DRBD设备的主备切换,把dbm137切换为seocndary,把dbm138切换为primary ##在dbm137(primary)端执行 [[email protected] ~]# /etc/init.d/mysqld stop #将137的mysql服务stop掉 Shutting down MySQL. SUCCESS! [[email protected] ~]# umount /dev/drbd0 #卸载DRBD设备 [[email protected] ~]# drbdadm secondary r0 #切换137为secondary [[email protected] ~]# cat /proc/drbd | grep ro version: 8.3.16 (api:88/proto:86-97) 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----
##在dbm138(secondary)端执行 [[email protected] ~]# drbdadm primary r0 #把138切换为primary [[email protected] ~]# cat /proc/drbd | grep ro version: 8.3.16 (api:88/proto:86-97) 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- [[email protected] ~]# mount /dev/drbd0 /data/ #挂载DRBD设备 [[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 19G 3.4G 14G 20% / tmpfs 58M 0 58M 0% /dev/shm /dev/sda1 477M 43M 409M 10% /boot /dev/drbd0 8.8G 2.1G 6.3G 25% /data [[email protected] ~]# cd /data/mysql/data/ #查看mysql数据 [[email protected] data]# ll total 2109468 -rw-rw---- 1 mysql mysql 56 Jul 2 12:46 auto.cnf -rw-rw---- 1 mysql mysql 12582912 Jul 2 13:02 ibdata1 -rw-rw---- 1 mysql mysql 1073741824 Jul 2 13:02 ib_logfile0 -rw-rw---- 1 mysql mysql 1073741824 Jul 2 12:46 ib_logfile1 drwx------ 2 mysql mysql 4096 Jul 2 12:00 mysql drwx------ 2 mysql mysql 4096 Jul 2 12:00 performance_schema -rw-rw---- 1 mysql mysql 185 Jul 2 12:46 slow.log drwx------ 2 mysql mysql 4096 Jul 2 12:00 test [[email protected] ~]# cd /data/mysql/binlog/ #查看二进制文件 [[email protected] binlog]# ll total 8 -rw-rw---- 1 mysql mysql 285 Jul 2 13:02 mysql-binlog.000001 -rw-rw---- 1 mysql mysql 39 Jul 2 12:46 mysql-binlog.index [[email protected] ~]# mv /usr/local/mysql/my.cnf /usr/local/mysql/my.cnf.old [[email protected] ~]# vi /usr/local/mysql/my.cnf #修改配置文件 [mysqld] basedir = /usr/local/mysql #安装路径 datadir = /data/mysql/data #数据目录 port = 3306 server_id = 1 #此值不要去修改,因为主备mysql同一时刻只有一个被启动 pid-file = /data/mysql/data/mysql.pid #进程文件 socket = /var/lib/mysql/mysql.sock default_storage_engine = InnoDB log-bin = /data/mysql/binlog/mysql-binlog #二进制文件 expire_logs_days = 14 max_binlog_size = 5G binlog_cache_size = 10M max_binlog_cache_size = 20M slow_query_log long_query_time = 2 slow_query_log_file = /data/mysql/data/slow.log #slow日志文件 open_files_limit = 65535 innodb = FORCE innodb_buffer_pool_size = 100M #注意这个参数 innodb_log_file_size = 1G query_cache_size = 0 thread_cache_size = 64 table_definition_cache = 512 table_open_cache = 512 max_connections = 20 sort_buffer_size = 10M max_allowed_packet = 6M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [client] #socket = /var/lib/mysql/mysql.sock ##注意: ##主备节点要保证配置文件my.cnf两边一模一样,避免出现问题 [[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #拷贝启动脚本 ##注意: ##千万不要,千万不要设置mysql开机自动启动,因为mysql服务的启动由heartbeat统一管理 [[email protected] ~]# /etc/init.d/mysqld start #启动mysql服务 Starting MySQL.................................... SUCCESS! [[email protected] ~]# ps -ef | grep mysql root 16284 1 0 12:46 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql.pid mysql 16783 16284 6 12:46 pts/1 00:00:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --open-files-limit=65535 --pid-file=/data/mysql/data/mysql.pid --socket=/var/lib/mysql/mysql.sock --port=3306 root 16878 1286 0 12:47 pts/1 00:00:00 grep mysql [[email protected] ~]# vi /etc/profile #修改PATH路径 ##在最后添加: export PATH=$PATH:/usr/local/mysql/bin [[email protected] ~]# source /etc/profile #立即生效
时间: 2024-10-25 15:36:50