mysql multiple instance mode (1) --- multiple configuration files

Posted by oughost on Fri, 03 Apr 2020 03:15:02 +0200

For the convenience of not starting from scratch, experiment directly on the existing database system, omit to install the database software (the method is similar), the following is an example of adding an instance

essential information:

[root@localhost ~]# cat /etc/issue
CentOS release 6.5 (Final)          

mysql> \s
Server version:         5.7.21 Source distribution      ---Source compilation and installation
Protocol version:       10

//Occupied by existing database instance3306port
[root@localhost ~]# netstat -anp | grep mysqld 
tcp        0      0 :::3306                     :::*                        LISTEN      70932/mysqld      
unix  2      [ ACC ]     STREAM     LISTENING     381292 70932/mysqld        /disk1/mysql/mysql.sock

1. principle

Through mysql program → call different configuration files (specify port, database directory, etc.) → modify script
To start multiple instances

2. Configuration steps

2.1 create database datadir directory

[root@localhost ~]# mkdir -pv  /disk1/mysql/data3307
[root@localhost ~]# chown  -R mysql.mysql /disk1/mysql/data3307/

2.2 initialize database

[root@localhost ~]# mysqld  --initialize-insecure --user=mysql --basedir=/disk1/mysql/  --datadir=/disk1/mysql/data3307
[root@localhost ~]# ll /disk1/mysql/data3307

2.3 create profile

Simply create a configuration file, mainly modify port,sockt,datadir

[root@localhost ~]# vi /etc/my3307.cnf
[mysqld]
#server character set
character-set-server=utf8
collation-server = utf8_general_ci
user=mysql
port = 3307
#Directory settings
basedir=/disk1/mysql
datadir=/disk1/mysql/data3307
# Client connection socket
socket=/disk1/mysql/mysql3307.sock
# solve the problem:TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true
# skip_grant_tables
[mysql]
socket=/disk1/mysql/mysql3307.sock
default-character-set=utf8
[mysql.server]
default-character-set=utf8
[mysql_safe]
default-character-set=utf8
[client]
socket=/disk1/mysql/mysql3307.sock
default-character-set=utf8

2.4 start and connect test

[root@localhost ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &
[root@localhost ~]# netstat -anp | grep mysql
tcp        0      0 :::3307                     :::*                        LISTEN      71865/mysqld        
tcp        0      0 :::3306                     :::*                        LISTEN      71357/mysqld        
unix  2      [ ACC ]     STREAM     LISTENING     383013 71357/mysqld        /disk1/mysql/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     383478 71865/mysqld        /disk1/mysql/mysql3307.sock

Connection (specify connection to 3307 instance)

[root@localhost ~]# mysql -h 127.0.0.1 -P 3307 -u root -p
[root@localhost ~]# mysql -S /disk1/mysql/mysql3307.sock  -u root -p
[root@localhost ~]# mysql --defaults-file=/etc/my3307.cnf

2.5 service script self start

[root@localhost ~]# vi /etc/init.d/mysqld3307
//The first47That's ok
datadir=/disk1/mysql/data       
//Change to
datadir=/disk1/mysql/data3307

//The first63That's ok
mysqld_pid_file_path=
//Change to 
mysqld_pid_file_path=$datadir/`hostname`.pid

//The first266That's ok
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
 //Modified to
$bindir/mysqld_safe  --defaults-file=/etc/my3307.cnf --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
[root@localhost ~]# chkconfig --add mysqld3307
[root@localhost ~]# chkconfig mysqld3307 on
[root@localhost ~]# service mysqld3307 restart

Topics: MySQL Database socket Unix