Openstack(四)Mysql主从
4.1 安装mysql
4.1.1 安装依赖
# yum install vim gcc gcc-c++ wget autoconf net-tools lrzsz iotop lsof iotop bash-completion –y
# yum install curl policycoreutils openssh-server openssh-clients postfix –y
4.1.2 下载mysql包:
# mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
# tar -xf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz -C /usr/local/src
# useradd mysql -s /sbin/nologin
# chown -R mysql.mysql /usr/local/mysql/* -R
# mkdir -pv /var/lib/mysql && chwn mysql.mysql /var/lib/mysql –R
# mkdir -pv /data/mysql/{data,logs} && touch /data/mysql/logs/error.log && chown -R mysql.mysql /data -R
4.1.3安装mysql
# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/data --#basedir=/usr/local/mysql/
# cp /usr/local/src/mysql-5.6.36-linux-glibc2.5-x86_64/support-files/mysql.server /etc/init.d/mysqld
# chmod a+x /etc/init.d/mysqld
# ln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql
4.1.4配置my.cnf
# vim /etc/my.cnf
[client]
port = 3306
socket = /data/mysql/data/mysql.sock
#default-character-set=utf8
[mysqld]
#skip-grant-tables
sync_binlog=0 # 同步binlog设置
innodb_flush_log_at_trx_commit=0
default-time-zone = '+8:00'
local-infile=0
skip-name-resolve
skip-external-locking
back_log = 300
max_connections = 1000 # 最大链接
max_allowed_packet = 32M
binlog_cache_size = 1M
max_heap_table_size = 64M
sort_buffer_size = 2M
query_cache_type=0
join_buffer_size = 2M
thread_cache = 8
ft_min_word_len = 4
thread_stack = 192K
tmp_table_size = 64M
port = 3306
socket = /data/mysql/data/mysql.sock
skip-external-locking
key_buffer_size = 16M
table_open_cache = 400
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
datadir=/data/mysql/data
log-bin=mysql-bin
replicate-ignore-db=test
replicate-ignore-db=performance_schema
replicate-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog_format=row
server-id =201 # 主从serviceid不能一样,重点
relay-log=relay-bin
relay-log-index=relay-bin
log-queries-not-using-indexes
long_query_time =1
slow_query_log=on
log_queries_not_using_indexes=off
slow_query_log_file=/data/mysql/logs/slow_query.log
log-error=/data/mysql/logs/error.log
key_buffer_size = 32M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 10M
myisam_repair_threads = 1
myisam_recover
innodb_buffer_pool_size=5000M
innodb_additional_mem_pool_size=32M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 16
innodb_log_buffer_size = 8M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table=1
innodb_open_files=500
character-set-server = utf8 # 指定字符编码
wait_timeout=300
interactive_timeout=300
innodb_flush_method=O_DIRECT
log-bin-trust-function-creators=1
log_slave_updates = 1
[mysqldump]
quick
max_allowed_packet = 16M
user=root
password=root
[mysql]
no-auto-rehash
# /etc/init.d/mysqld start
4.1.5 mysql安装脚本:
#ll
-rw-r--r-- 1 root root 310425313 Mar 17 18:12 mysql-5.6.36-onkeyinstall.tar.gz
-rwxr-xr-x 1 root root 1513 Jan 22 16:20 mysql-install.sh
-rw-r--r-- 1 root root 2293 Jan 22 16:17 my.cnf
# ./mysql-install.sh
# cat mysql-install.sh
#!/bin/bash
DIR=`pwd`
NAME="mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz"
FULL_NAME=${DIR}/${NAME}
DATA_DIR="/data/mysql"
yum install vim gcc gcc-c++ wget autoconf net-tools lrzsz iotop lsof iotop bash-completion -y
yum install curl policycoreutils openssh-server openssh-clients postfix -y
if [ -f ${FULL_NAME} ];then
echo "安装文件存在"
else
echo "安装文件不存在"
exit 3
fi
if [ -h /usr/local/mysql ];then
echo "Mysql 已经安装"
exit 3
else
tar xvf ${FULL_NAME} -C /usr/local/src
ln -sv /usr/local/src/mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
if id mysql;then
echo "mysql 用户已经存在,跳过创建用户过程"
fi
useradd mysql -s /sbin/nologin
if id mysql;then
chown -R mysql.mysql /usr/local/mysql/* -R
if [ ! -d /data/mysql ];then
mkdir -pv /var/lib/mysql && chwn mysql.mysql /var/lib/mysql -R
mkdir -pv /data/mysql/{data,logs} && touch /data/mysql/logs/error.log && chown -R mysql.mysql /data -R
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/data --basedir=/usr/local/mysql/
cp /usr/local/src/mysql-5.6.36-linux-glibc2.5-x86_64/support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
cp ${DIR}/my.cnf /etc/my.cnf
ln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql
/etc/init.d/mysqld start
else
echo "MySQL数据目录已经存在,"
exit 3
fi
fi
4.2 配置mysql主从
4.2.1主库配置授权账号并查看状态
# /etc/init.d/mysqld restart
# mysql
> create database testdatabase;
> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'tom'@'192.168.10.%' IDENTIFIED BY '123456'; #授权tom从192.168.10.*使用密码123456同步数据
#
> show master status;
>exit
4.2.2主库dump导从库
# 主库执行
# /usr/local/mysql/bin/mysqldump --all-databases --single_transaction --flush-logs --master-data=2 --lock-tables > /backup.sql
# scp /backup.sql 192.168.10.202:/root/
# 从库执行
# /usr/local/mysql/bin/mysql < /root/backup.sql # 创建账号的权限已导入从库
4.2.3配置从库同步主库
# mysql
> CHANGE MASTER TO MASTER_HOST='192.168.10.101',MASTER_USER='tom',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000006',MASTER_LOG_POS=120;
> start slave;
> show global variables like "%read_only%";
> set global read_only=1; #开启只读模式,只读模式仅对普通用户生效,对root不生效
> show global variables like "%read_only%"; #验证是否生效
4.3验证mysql主从
> show slave status\G;
Slave_IO_Running: Yes #这两个线程状态必须为YES才表示同步是成功的
Slave_SQL_Running: Yes