Openstack(四)Mysql主從

 

 

4.1 安裝mysql

4.1.1 安裝依賴

# yum install vim gcc gcc-c++ wget autoconf  net-tools lrzsz iotop lsof iotop bash-completion –ymysql

# yum install curl policycoreutils openssh-server openssh-clients postfix –ylinux

4.1.2 下載mysql包:

# mysql-5.6.36-linux-glibc2.5-x86_64.tar.gzc++

# tar -xf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz -C /usr/local/srcsql

# useradd  mysql  -s /sbin/nologinvim

# chown  -R mysql.mysql  /usr/local/mysql/* -Rbash

# mkdir -pv /var/lib/mysql && chwn mysql.mysql /var/lib/mysql –Rssh

# mkdir -pv /data/mysql/{data,logs} && touch  /data/mysql/logs/error.log && chown  -R mysql.mysql  /data   -Rcurl

4.1.3安裝mysql

# /usr/local/mysql/scripts/mysql_install_db  --user=mysql --datadir=/data/mysql/data  --#basedir=/usr/local/mysql/socket

# cp  /usr/local/src/mysql-5.6.36-linux-glibc2.5-x86_64/support-files/mysql.server /etc/init.d/mysqldpost

# 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

相關文章
相關標籤/搜索