環境說明:本環境由兩台mysql 資料庫和heartbeat 組成,一台的ip 為192.168.10.197,一台為192.168.10.198,對外提供服務的vip 為192.168.10.200
備忘:heartbeat 本身是不能做到服務不可用自動切換的,所以用結合額外的指令碼才可以做到,本文中提到的moniter 指令碼即為實現某個mysql 服務停用時候自動切換的還可以自動警示
安裝和配置過程分為如下幾步:
第一部分:mysql 的安裝配置
1 安裝
1.1. 添加mysql 運行所需的使用者和組
groupadd mysql
useradd -g mysql mysql
1.2. 解壓安裝
tar mysql-5.1.26-rc-linux-x86_64-glibc23.tar.gz
mv mysql-5.1.26-rc-linux-x86_64-glibc23 /usr/local/mysql/
chown -R mysql:mysql /usr/local/mysql
1.3. 複製主設定檔和啟動指令碼
cd /usr/local/mysql
cp support-files/my-medium.cnf /etc/my.cf
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
1.4. 初始化資料庫
cd /usr/local/mysql
scripts/mysql_install_db --user=mysql
1.5. 更改資料目錄的所有者和組
2
chown -R mysql:mysql ./data
1.6. 註冊mysql 為系統服務
chkconfig --add mysqld
chkconfig --levels 2345 mysqld on
1.7. 輸出環境變數
#Vi /etc/profile(內容如下)
PATH=$PATH:/usr/local/mysql/bin
# source /etc/profile
1.8. 設定資料庫密碼
service mysqld start
mysqladmin password 123456(密碼自己定這裡只是個例子)
1.9. 開啟root 遠端存取許可權
mysql –p123456
mysql> grant all on *.* to root@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
mysql> quit
2 參數設定
2.1. 建立相關目錄並設定許可權
mkdir /usr/local/mysql/binlog/
chown –R mysql:mysql /usr/local/mysql/binlog/
touch /var/log/mysql.log
chown –R mysql:mysql /var/log/mysql.log
2.2. 添加同步複製的帳號(主庫和輔庫的設定相同)
[root@master ~]# mysql -p
Enter password:(輸入root 密碼)
mysql> grant all on *.* to qiangao identified by '123456';(其中的帳號和密碼要
和設定檔中指定的相同)
mysql> flush privileges;
2.3. 修改主設定檔
3
注意:其中ip 要互相指向對方的ip 按實際情況來設定,使用者名稱和密碼要和上文的使用者名稱密
碼保持一致
主庫設定
vi /etc/my.cnf(用如下內容覆蓋原有內容)
################################################
##########
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
################### auto_increment
###########################
auto_increment_offset = 1
auto_increment_increment = 2
############## other options ##############
default-character-set = utf8
default-storage-engine = InnoDB
default-table-type = INNODB
max_connections = 800
port = 3306
socket = /tmp/mysql.sock
skip-locking
########## MyISAM options #################
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_sort_buffer_size = 10M
myisam_repair_threads = 1
################ select cache options ##################
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 5M
4
max_allowed_packet = 1M
table_cache = 2048
query_cache_size = 32M
query_cache_limit = 2M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_concurrency = 8
################ index cache options ##################
key_buffer_size = 32M
key_buffer_size = 32M
################# master #########################
server-id = 1
log-bin=/usr/local/mysql/binlog/master-bin
binlog_format=mixed
relay-log=/usr/local/mysql/binlog/mysqld-relay-bin
##################### slave
##########################
relay-log=/usr/local/mysql/binlog/slave-relay-bin
master-host=192.168.10.198
master-user=qiangao
master-password=123456
master-connect-retry=10
############## log ##################
log-error=/var/log/mysql.log
######### INNODB #########
innodb_file_per_table
#+++++++ log ++++++++#
innodb_log_buffer_size = 10M
innodb_mirrored_log_groups = 1
innodb_log_files_in_group = 3
5
innodb_log_file_size = 50M
innodb_flush_log_at_trx_commit = 0
#innodb_log_archive = 0
#+++++++ System buffer +++++++#
innodb_buffer_pool_size = 1024M
innodb_additional_mem_pool_size = 40M
#+++++++ Other +++++++#
innodb_file_io_threads = 4
innodb_lock_wait_timeout = 5
innodb_force_recovery = 0
innodb_fast_shutdown = 1
innodb_thread_concurrency = 8
innodb_lock_wait_timeout = 50
transaction-isolation = READ-COMMITTED
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M