MySQL-5.5.49安裝、多實例、主從複製

源碼安裝mysql

yum install ncurses-devel libaio-devel -y
mkdir /server/tools -p
cd /server/tools
wget https://cmake.org/files/v2.8/cmake-2.8.8.tar.gz
tar xf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
gmake 
gmake install
which cmake
/usr/local/bin/cmake
cd ..
useradd -s /sbin/nologin -M mysql
wget https://downloads.mysql.com/archives/get/file/mysql-5.5.49.tar.gz
tar xf mysql-5.5.49.tar.gz
cd mysql-5.5.49
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.49 \
-DMYSQL_DATADIR=/application/mysql-5.5.49/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.49/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0

make 
make install
ln -s /application/mysql-5.5.49/ /application/mysql
ls /application/mysql

 二進制包安裝mysql

useradd -s /sbin/nologin -M mysql
mkdir /application 
mkdir /server/tools -p
cd /server/tools
wget https://downloads.mysql.com/archives/get/file/mysql-5.5.49-linux2.6-x86_64.tar.gz
tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz
mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
ln -s /application/mysql-5.5.49/ /application/mysql
sed -i 's#/usr/local/#/application/#g' /application/mysql/bin/mysqld_safe
cd /application/mysql

 建立多實例配置文件和啓動腳本

mkdir -p /data/{3306,3307}/data

vim /data/3306/my.cnf       #3306配置文件

[client]
port            = 3306
socket          = /data/3306/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user            = mysql
port            = 3306
socket          = /data/3306/mysql.sock
basedir         = /application/mysql
datadir         = /data/3306/data
open_files_limit = 1024
back_log        = 600
max_connections  = 800
max_connect_errors = 3000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
#default_table_type = InnoDB
thread_stack = 192K
#transaction_isolation = READ-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
pid-file = /data/3306/mysql.pid
relay-log = /data/3306/relay-bin
relay-log-info-file = /data/3306/relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db = mysql

server-id = 1

innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 4M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0

[mysqldump]
quick
max_allowed_packet = 2M

[mysqld_safe]
log-error=/data/3306/mysql_peter3306.err
pid-file=/data/3306/mysqld.pid

vim /data/3307/my.cnf       #3307配置文件

[client]
port            = 3307
socket          = /data/3307/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user            = mysql
port            = 3307
socket          = /data/3307/mysql.sock
basedir         = /application/mysql
datadir         = /data/3307/data
open_files_limit = 1024
back_log        = 600
max_connections  = 800
max_connect_errors = 3000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
#default_table_type = InnoDB
thread_stack = 192K
#transaction_isolation = READ-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
pid-file = /data/3307/mysql.pid
relay-log = /data/3307/relay-bin
relay-log-info-file = /data/3307/relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db = mysql

server-id = 3

innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 4M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0

[mysqldump]
quick
max_allowed_packet = 2M

[mysqld_safe]
log-error=/data/3307/mysql_peter3307.err
pid-file=/data/3307/mysqld.pid

vim /data/3306/mysql       #3306啓動腳本

#!/bin/bash
####################################
#this scripts is created by peter at 2017-08-31
#mail:asdftttt@163.com
####################################
#init
port=3306
mysql_user="root"
mysql_pwd="123456"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_start_mysql(){
     if [ ! -e "$mysql_sock" ];then
          printf "Starting MySQL...\n"
          /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
     else
          printf "MySQL is running...\n"
          exit
     fi
}

#stop function
function_stop_mysql(){
     if [ ! -e "$mysql_sock" ];then
          printf "MySQL is stopped...\n"
          exit
     else 
          printf "Stopping MySQL...\n"
          ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
     fi
}

#restart function
function_restart_mysql(){
     printf "Restarting MySQL...\n"
     function_stop_mysql
     sleep 2
     function_start_mysql
}

case $1 in
start)
     function_start_mysql
;;
stop)
     function_stop_mysql
;;
restart)
     function_restart_mysql
;;
*)
     printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

vim /data/3307/mysql       #3307啓動腳本

#!/bin/bash
####################################
#this scripts is created by peter at 2017-08-31
#mail:asdftttt@163.com
####################################
#init
port=3307
mysql_user="root"
mysql_pwd="123456"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_start_mysql(){
     if [ ! -e "$mysql_sock" ];then
          printf "Starting MySQL...\n"
          /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
     else
          printf "MySQL is running...\n"
          exit
     fi
}

#stop function
function_stop_mysql(){
     if [ ! -e "$mysql_sock" ];then
          printf "MySQL is stopped...\n"
          exit
     else
          printf "Stopping MySQL...\n"
          ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
     fi
}

#restart function
function_restart_mysql(){
     printf "Restarting MySQL...\n"
     function_stop_mysql
     sleep 2
     function_start_mysql
}

case $1 in
start)
     function_start_mysql
;;
stop)
     function_stop_mysql
;;
restart)
     function_restart_mysql
;;
*)
     printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

tree /data
/data
├── 3306
│   ├── data
│   ├── my.cnf
│   └── mysql
└── 3307
    ├── data
    ├── my.cnf
└── mysql

 配置文件權限,將mysql客戶端命令加入路徑

chown -R mysql.mysql /data
find /data -type f -name mysql | xargs chmod 700       // 啓動文件中有數據庫管理員密碼
find /data -type f -name mysql -exec ls -l {} \;
-rwx------ 1 mysql mysql 1203 Sep  2 21:12 /data/3307/mysql
-rwx------ 1 mysql mysql 1204 Sep  2 21:12 /data/3306/mysql
ln -s /application/mysql/bin/* /usr/local/sbin

 初始化數據庫,啓動多實例,加入開機啓動

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
chown -R mysql.mysql /data
/data/3306/mysql start
/data/3307/mysql start
netstat -nutlp | grep 330
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      27934/mysqld       
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      28653/mysqld 
echo "#mysql multi instances" >> /etc/rc.local
echo "/data/3306/mysql start" >> /etc/rc.local
echo "/data/3307/mysql start" >> /etc/rc.local
tail -3 /etc/rc.local
#mysql multi instances
/data/3306/mysql start
/data/3307/mysql start

 登陸不一樣實例,設置管理員密碼

mysql -S /data/3306/mysql.sock
mysql -S /data/3307/mysql.sock
mysqladmin -u root -S /data/3306/mysql.sock password '123456'
mysqladmin -u root -S /data/3307/mysql.sock password '123456'
mysql -uroot -p123456 -S /data/3306/mysql.sock
mysql -uroot -p123456 -S /data/3306/mysql.sock

 如何再增長一個實例?

#多實例本質是多端口,多配置文件。添加的流程是改配置,改啓動腳本,初始化,給權限,啓動
mkdir -p /data/3308/data \cp /data/3306/my.cnf /data/3308/ \cp /data/3306/mysql /data/3308/ sed -i 's#3306#3308#g' /data/3308/my.cnf sed -i 's#server-id = 1#server-id = 8#g' /data/3308/my.cnf sed -i 's#3306#3308#g' /data/3308/mysql chmod 700 /data/3308/mysql /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3308/data --user=mysql chown -R mysql.mysql /data/3308 /data/3308/mysql start echo "/data/3308/mysql start" >> /etc/rc.local netstat -nutlp | grep 3308 tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 29518/mysqld

MySQL主從複製

MySQL的主從複製是MySQL自帶的功能,經過邏輯的binlog日誌來實現同步。MySQL支持單向、雙向、鏈式、環狀等主從複製方式。php

主從複製有利於數據庫架構的健壯性、提高訪問速度,同時主從服務器能夠互爲備份。java

此外,主從複製還可實現讀寫分離,即主庫寫從庫讀,從庫作集羣。通常企業實現讀寫分離的方式以下:mysql

中大型公司:經過程序(php,java),根據業務拆分從服務器
測試環境:代理軟件(mysql-proxy,amoeba)
門戶網站:分佈式dbproxy(讀寫分離,hash負載均衡,健康檢查)

MySQL主從複製原理

1.Slave上執行start slave開始主從複製。
2.Slave上IO線程根據CHANGE MASTER命令配置的用戶權限請求鏈接Master,而後請求Master從指定binlog日誌文件的指定位置開始發送binlog日誌內容。
3.Master上IO線程收到請求,根據請求信息讀取指定的binlog日誌並返回給Slave,同時還返回Master上新的binlog文件和下一個更新位置。
4.Slave上IO線程收到binlog日誌內容和位置信息後,將binlog數據寫入relaylog(中繼日誌)文件,將新的binlog文件名和位置信息記錄到master-info,以便下次請求日誌時告訴Master請求的位置。
5.Slave上SQL線程檢測到relaylog中日誌更新後,順序執行日誌中的SQL語句,並在relay-log.info中記錄當前執行到的文件名和位置,便於分段執行。
 
注:數據庫主從複製是異步的基於SQL語句的邏輯複製,複製的前提是Master上開啓binlog日誌記錄功能,複製的時候Master上有一個IO線程,Slave上有IO和SQL兩個線程,Master和Slave的server-id不能相同,binlog日誌只記錄對數據庫有更改的SQL語句,不記錄查詢語句。

單機多實例主從複製

主庫:Master 3306
從庫:Slave1 3307
從庫:Slave2 3308

#Master
vim /data/3306/my.cnf

[mysqld]
...
server-id = 1
log-bin = /data/3306/mysql-bin

/data/3306/mysql restart

mysql -uroot -p123456 -S /data/3306/mysql.sock
mysql>show variables like 'server_id';
mysql>show variables like 'log_bin';
mysql>grant replication slave on *.* to 'rep'@'172.16.1.%' identified by '123456';
mysql>flush privileges;
mysql>select user,host from mysql.user;
mysql>show grants for 'rep'@'172.16.1.%';
mysql>flush table with read lock;  // 窗口不能關閉,關閉自動解鎖
mysql>show master status;

#新開窗口
mkdir /server/backup/ -p
mysqldump -uroot -p123456 -S /data/3306/mysql.sock --events -A -B | gzip > /server/backup/mysql_bak.$(date +%F).sql.gz
ls -l /server/backup

mysql>show master status;
mysql>unlock tables;

#Slave1
vim /data/3307/my.cnf

[mysqld]
...
server-id = 3  // id必須惟一

/data/3307/mysql restart

cd /server/backup
ls -l
gzip -d mysql_bak.2017-09-03.sql.gz   
mysql -uroot -p123456 -S /data/3307/mysql.sock < mysql_bak.2017-09-03.sql
mysql -uroot -p123456 -S /data/3307/mysql.sock << EOF
CHANGE MASTER TO
MASTER_HOST='172.16.1.34',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=332;
EOF
cat /data/3307/data/master.info
mysql -uroot -p123456 -S /data/3307/mysql.sock -e "start slave;"
mysql -uroot -p123456 -S /data/3307/mysql.sock -e "show slave status\G;" | egrep "IO_Running|SQL_Running|_Behind_Master"

#Master
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "create database peter;"

#Slave1
mysql -uroot -p123456 -S /data/3307/mysql.sock -e "show databases like 'peter';"

 注:1.mysqldump會自動鎖表linux

#啓動並打開general_log,咱們能夠看到mysqldump登陸數據庫後首先執行的就是鎖表。
#FLUSH TABLS 是爲了等待數據庫全部寫操做結束,這樣再鎖表會快一些。
mysql> set global general_log=on;
mysql> show variables like '%general_log_file%';
+------------------+----------------------------+
| Variable_name    | Value                      |
+------------------+----------------------------+
| general_log_file | /data/3306/data/test88.log |
+------------------+----------------------------+
1 row in set (0.00 sec)

[root@test88 data]# vim test88.log
170918 17:35:29    11 Connect   root@localhost on
                   11 Query     /*!40100 SET @@SQL_MODE='' */
                   11 Query     /*!40103 SET TIME_ZONE='+00:00' */
                   11 Query     FLUSH /*!40101 LOCAL */ TABLES
                   11 Query     FLUSH TABLES WITH READ LOCK
                   11 Query     SHOW MASTER STATUS
                   11 Query     SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM 
INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IN (SELECT DISTINCT
LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT
TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='peter' AND TABLE_NAME IN ('first'))) GROUP BY
LOGFILE_GROUP_NAME, FILE_NAME, ENGINE ORDER BY LOGFILE_GROUP_NAME

 

2.使用--master-data參數自動給Slave設置Master上binlog文件和位置
 
咱們知道mysqldump也是基於SQL語句的邏輯備份,因此導入備份的過程實際上就是在執行SQL語句。--master-data參數會自動在備份文件中生成 CHANGE MASTER語句,導入備份時會自動執行。顯然咱們能夠經過mysqldump --master-data方式代替手動去獲取和設置binlog日誌名和位置信息。
 
當master-data=2時,生成的是註釋,不會執行。
mysqldump -uroot -p123456 -S /data/3306/mysql.sock --events -A -B --master-data=2 > /opt/rep.sql;
vim /opt/rep.sql

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=332;
master-data=1 時,生成的是SQL語句,會自動執行。
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=417;
相關文章
相關標籤/搜索