MySQL單機多執行個體配置實戰

來源:互聯網
上載者:User

標籤:linux 營運 linux營運工程師

本文主要介紹在CentOS6.9下快速安裝和部署MySQL多執行個體


基本流程:

1.Mysql多執行個體介紹

2.安裝MySQL多執行個體

3.建立MySQL多執行個體的資料檔案目錄以及設定檔

4.建立MySQL多執行個體的開機檔案

5.設定檔許可權和軟連結

6.初始化MySQL多執行個體的資料庫檔案

7.啟動MySQL多執行個體資料庫

8.配置和管理MySQL多執行個體資料庫



流程一:Mysql多執行個體介紹


  簡單的說,Mysql多執行個體就是在一台伺服器上同時開啟多個不同的服務連接埠(如 : 3306/3307/3308)同時運行多個Mysql伺服器,這些服務進程通過不同的socket來監聽不同的服務連接埠來提供服務

  這些Mysql多執行個體共用一套Mysql安裝程式,使用不同的my.cnf設定檔,啟動程式,和資料檔案,在提供服務時,多執行個體Mysql在邏輯上來看是各自獨立的,他們根據設定檔對應設定值,獲得伺服器相應的資源



流程二:安裝MySQL多執行個體


1)安裝MySQL所需的依賴包

yum install ncurses-devel libaio-devel -y

yum install cmake -y


2)擷取MySQL二進位包以及安裝

useradd -s /sbin/nologin  -M mysql

id mysql

wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.32-linux2.6-x86_64.tar.gz 解壓並建立軟串連(這是二進位安裝包,僅需要解壓就可以,不需要cmake/configure,make&&make install等過程)

tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz

mkdir -p /application/

mv mysql-5.5.32-linux2.6-x86_64  /application/mysql-5.5.32

ln -s /application/mysql-5.5.32/ /application/mysql

cd /application/mysql

ls -l support-files/*.cnf



流程三:建立MySQL多執行個體的資料檔案目錄以及設定檔



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


cd /data/3306

[[email protected] 3306]# cat my.cnf 

[mysqld]

basedir=/application/mysql

datadir=/data/3306

socket=/data/3306/mysql.sock

log-error=/data/3306/mysqlerr.log

log_bin=/data/3306/mysql-bin

binlog_format=row

skip_name_resolve=1

server_id=3306

port=3306


cd /data/3307

[[email protected] 3307]# cat my.cnf 

[mysqld]

basedir=/application/mysql

datadir=/data/3307

socket=/data/3307/mysql.sock

log-error=/data/3307/mysqlerr.log

log_bin=/data/3307/mysql-bin

binlog_format=row

skip_name_resolve=1

server_id=3307

port=3307

#實際工作中我們都是拿早已配置好的模板來進行修改的,這裡用醉簡單的模板來做示範



流程四:建立MySQL多執行個體的開機檔案

cd /data/3306

[[email protected] 3306]# cat mysqld

. /etc/init.d/functions

. /etc/profile


Start='/application/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf --pid-file=/data/3306/3306.pid'

Stop='mysqladmin -uroot -S /data/3306/mysql.sock shutdown'

Port=`ss -tunlp|grep 3306|wc -l`

  

function START(){

 if [ $Port -ne 1 ];then

  $Start >/dev/null 2>&1 &

  sleep 3

  if [ $? -eq 0 ];then

   action 'MySQL 3306 Starting' /bin/true

  fi

 else

  action 'MySQL 3306 Already Exists' /bin/true

 fi

}

function STOP(){

 if [ $Port -ne 0 ];then

  $Stop

  if [ $? -eq 0 ];then

   action 'MySQL Stoping Successfuly' /bin/true

  fi

 else

  action 'MySQL already Stoped' /bin/true

 fi

}

function RESTART(){

 STOP

 sleep 1

 START

}

case $1 in

start)

 START

 ;;

stop)

 STOP

 ;;

restart)

 RESTART

 ;;

*)

 echo "Usage: $0 {start|stop|restart}"

 ;;

esac


cd /data/3307

[[email protected] 3307]# cat mysqld


. /etc/init.d/functions

. /etc/profile


Start='/application/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf --pid-file=/data/3307/3307.pid'

Stop='mysqladmin -uroot -S /data/3307/mysql.sock shutdown'

Port=`ss -tunlp|grep 3307|wc -l`

  

function START(){

 if [ $Port -ne 1 ];then

  $Start >/dev/null 2>&1 &

  sleep 3

  if [ $? -eq 0 ];then

   action 'MySQL 3307 Starting' /bin/true

  fi

 else

  action 'MySQL 3307 Already Exists' /bin/true

 fi

}

function STOP(){

 if [ $Port -ne 0 ];then

  $Stop

  if [ $? -eq 0 ];then

   action 'MySQL Stoping Successfuly' /bin/true

  fi

 else

  action 'MySQL already Stoped' /bin/true

 fi

}

function RESTART(){

 STOP

 sleep 1

 START

}

case $1 in

start)

 START

 ;;

stop)

 STOP

 ;;

restart)

 RESTART

 ;;

*)

 echo "Usage: $0 {start|stop|restart}"

 ;;

esac



流程五:設定檔許可權和軟連結

[[email protected] 3307]# chown -R mysql.mysql /data/

[[email protected] 3307]# find /data/ -name mysqld|xargs ls -l

-rw-r--r--. 1 mysql mysql 794 May  3 11:55 /data/3306/mysqld

-rw-r--r--. 1 mysql mysql 794 May  3 11:55 /data/3307/mysqld

[[email protected] 3307]# find /data/ -name mysqld|xargs chmod 700

[[email protected] 3307]# ln -s  /application/mysql/bin/*  /usr/local/sbin/




流程六:初始化MySQL多執行個體的資料庫檔案

cd /application/mysql/scripts

./mysql_install_db  --basedir=/application/mysql --datadir=/data/3306 --user=mysql

./mysql_install_db  --basedir=/application/mysql --datadir=/data/3307 --user=mysql

初始化資料庫後可以看到對應執行個體目錄下會多了如下檔案,省略部分

[[email protected] scripts]# tree /data/|head -10

/data/

├── 3306

│   ├── data

│   ├── my.cnf

│   ├── mysql

│   │   ├── columns_priv.frm

│   │   ├── columns_priv.MYD

│   │   ├── columns_priv.MYI

│   │   ├── db.frm

│   │   ├── db.MYD



流程七:啟動MySQL多執行個體資料庫

sed -i "s#/usr/local/mysql#/application/mysql#g"  /application/mysql/bin/mysqld_safe           #修改指令碼執行路徑

[[email protected] scripts]# /data/3306/mysqld start

MySQL 3306 Starting                                        [  OK  ]

[[email protected] scripts]# /data/3307/mysqld start 

MySQL 3306 Starting                                        [  OK  ]

[[email protected] 3307]# netstat -tnlp | grep 330

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      7983/mysqld         

tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      8271/mysqld 




流程八:配置和管理MySQL多執行個體資料庫


1)加入開機自啟

[[email protected] 3306]# echo "#mysql  multi instances" >> /etc/rc.local 

[[email protected] 3306]# echo "/data/3306/mysqld start" >> /etc/rc.local          

[[email protected] 3306]# echo "/data/3307/mysqld start" >> /etc/rc.local  


2)登入MySQL測試

[[email protected] 3306]# mysql -S /data/3306/mysql.sock         #mysql.sock用於區分登入不同的執行個體

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.32-log MySQL Community Server (GPL)


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| data               |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.01 sec)


mysql> select user();

+----------------+

| user()         |

+----------------+

| [email protected] |

+----------------+

1 row in set (0.00 sec)


3)MySQL多執行個體資料庫的管理方法

無密碼情況下登入資料庫方法:

mysql -S /data/3306/mysql.sock

mysql -S /data/3307/mysql.sock

重啟對應執行個體資料庫的命令:

/data/3306/mysqld restart


4)MySQL安全配置

通過mysqladmin命令為MySQL不同執行個體的資料庫設定獨立的密碼,命令如下:

[[email protected] 3306]# mysqladmin -u root -S /data/3306/mysql.sock password 'ywxi123'

[[email protected] 3306]# mysqladmin -u root -S /data/3307/mysql.sock password 'ywxi123'

[[email protected] 3306]# mysql -uroot -pywxi123 -S /data/3306/mysql.sock 

[[email protected] 3306]# mysql -uroot -pywxi123 -S /data/3307/mysql.sock 


5)再增加一個MySQL的執行個體

mkdir -p /data/3308/data

\cp /data/3306/mysqld /data/3308/

\cp /data/3306/my.cnf /data/3308/

sed -i 's/3306/3308/g' /data/3308/my.cnf

sed -i 's/3306/3308/g' /data/3308/mysqld 

chmod 700 /data/3308/mysqld

cd /application/mysql/scripts/

./mysql_install_db  --basedir=/application/mysql --datadir=/data/3308 --user=mysql

chown -R mysql:mysql /data/3308/

egrep "server_id|log_bin" /data/3308/my.cnf

 /data/3308/mysqld start

netstat -tnlp | grep 3308










MySQL單機多執行個體配置實戰

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.