This article mainly introduces the rapid installation and deployment of MySQL multi-instance under CentOS6.9
Basic process:
1. Mysql multi-instance introduction
2. Install MySQL multi-instance
3. Create MySQL multi-instance data file directory and configuration file
4. Create a startup file for MySQL multi-instance
5. Configure file permissions and soft links
6. Initialize the database file of MySQL multi-instance
7. Start MySQL multi-instance database
8. Configure and manage MySQL multi-instance databases
Process 1: Introduction to Mysql Multiple Instances
Simply put, Mysql multi-instance is to open multiple different service ports (such as: 3306/3307/3308) on one server at the same time and run multiple Mysql servers at the same time. These service processes listen to different service ports through different sockets. to provide services
These multiple instances of Mysql share a set of Mysql installation programs, using different my.cnf configuration files, startup programs, and data files. When providing services, multiple instances of Mysql are logically independent, and they are set according to the configuration files. Set the value to obtain the corresponding resources of the server
Process 2: Install MySQL multi-instance
1) Install the dependencies required by MySQL
yum install ncurses-devel libaio-devel -y
yum install cmake -y
2) Get MySQL binary package and install
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 Unzip and create a soft link (this is a binary installation package, you only need to unzip it Yes, no need for cmake/configure, make&&make install, etc.)
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
Process 3: Create MySQL multi-instance data file directory and configuration file
mkdir -p /data/{3306,3307}/data
cd / data / 3306
[root@localhost 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
[root@localhost 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
#In actual work, we use the templates that have already been configured for modification. Here we use a simple template for demonstration.
Process 4: Create a startup file for MySQL multi-instance
cd / data / 3306
[root@localhost 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
be
else
action 'MySQL 3306 Already Exists' /bin/true
be
}
function STOP(){
if [ $Port -ne 0 ];then
$Stop
if [ $? -eq 0 ];then
action 'MySQL Stoping Successfuly' /bin/true
be
else
action 'MySQL already Stoped' /bin/true
be
}
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
[root@localhost 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
be
else
action 'MySQL 3307 Already Exists' /bin/true
be
}
function STOP(){
if [ $Port -ne 0 ];then
$Stop
if [ $? -eq 0 ];then
action 'MySQL Stoping Successfuly' /bin/true
be
else
action 'MySQL already Stoped' /bin/true
be
}
function RESTART(){
STOP
sleep 1
START
}
case $1 in
start)
START
;;
stop)
STOP
;;
restart)
RESTART
;;
*)
echo "Usage: $0 {start|stop|restart}"
;;
esac
Process 5: Configure file permissions and soft links
[root@localhost 3307]# chown -R mysql.mysql /data/
[root@localhost 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
[root@localhost 3307]# find /data/ -name mysqld|xargs chmod 700
[root@localhost 3307]# ln -s /application/mysql/bin/* /usr/local/sbin/
Process 6: Initialize the database file of MySQL multi-instance
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
After initializing the database, you can see that the following files will be added to the corresponding instance directory, omitting the part
[root@localhost scripts]# tree /data/|head -10
/data/
├── 3306
│ ├── data
│ ├── my.cnf
│ ├── mysql
│ │ ├── columns_priv.frm
│ │ ├── columns_priv.MYD
│ │ ├── columns_priv.MYI
│ │ ├── db.frm
│ │ ├── db.MYD
Process 7: Start the MySQL multi-instance database
sed -i "s#/usr/local/mysql#/application/mysql#g" /application/mysql/bin/mysqld_safe #Modify the script execution path
[root@localhost scripts]# /data/3306/mysqld start
MySQL 3306 Starting [ OK ]
[root@localhost scripts]# /data/3307/mysqld start
MySQL 3306 Starting [ OK ]
[root@localhost 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
Process 8: Configure and manage MySQL multi-instance databases
1) Join the boot auto-start
[root@localhost 3306]# echo "#mysql multi instances" >> /etc/rc.local
[root@localhost 3306]# echo "/data/3306/mysqld start" >> /etc/rc.local
[root@localhost 3306]# echo "/data/3307/mysqld start" >> /etc/rc.local
2) Log in to MySQL test
[root@localhost 3306]# mysql -S /data/3306/mysql.sock #mysql.sock is used to distinguish different instances of login
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() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
3) MySQL multi-instance database management method
Log in to the database without a password:
mysql -S /data/3306/mysql.sock
mysql -S /data/3307/mysql.sock
The command to restart the corresponding instance database:
/data/3306/mysqld restart
4) MySQL security configuration
Use the mysqladmin command to set independent passwords for the databases of different MySQL instances. The commands are as follows:
[root@localhost 3306]# mysqladmin -u root -S /data/3306/mysql.sock password 'ywxi123'
[root@localhost 3306]# mysqladmin -u root -S /data/3307/mysql.sock password 'ywxi123'
[root@localhost 3306]# mysql -uroot -pywxi123 -S /data/3306/mysql.sock
[root@localhost 3306]# mysql -uroot -pywxi123 -S /data/3307/mysql.sock
5) Add another instance of 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