Centos 7 deploys two mysql 8.0.31 (very detailed)

This article deploys two mysql instances on a centos 7 machine at the same time, and their port numbers are: 3306, 3307! 

Don't talk nonsense, let's do it! ! !

1. Preparatory work

1. mysql- boost -8.0.31.tar.gz source code download

        Download address: https://dev.mysql.com 

2. Configure Alibaba Cloud mirror source

cd /etc/yum.repos.d 
vi scl.repo 
 
# scl.repo文件内容如下:
 
[scl] 
name=CentOS-7 - SCLo sclo 
baseurl=http://mirrors.aliyun.com/centos/7/sclo/x86_64/rh/ 
gpgcheck=0 
enabled=1 

3. Install gcc, cmake, Boost C++ library, ncurses library, OpenSSL library 

 Note: cmake3, gcc-5.3 or above are required;

  The installation command is as follows:

yum install devtoolset-7-gcc devtoolset-7-gcc-c++ 
source /opt/rh/devtoolset-7/enable 
gcc -v   //查看gcc版本
 
yum install epel* 
yum clean all 
yum makecache 
yum install cmake3 ncurses ncurses-devel bison openssl*

2. Deployment preparation

1. Transfer mysql-boost-8.0.31.tar.gz to the /usr/local/ directory and decompress it

tar zxvf mysql-boost-8.0.31.tar.gz

2. Create multiple mysql installation directories in the /usr/local/ directory

# 端口号为 3306 的 mysql 安装目录
mkdir mysql3306

# 端口号为 3307 的 mysql 安装目录
mkdir mysql3307

3. Go to the cd mysql-8.0.31 directory and create the compilation directories of 3306 and 3307

cd mysql-8.0.31

mkdir bld3306

mkdir bld3307

3. Deploy the first mysql --- 3306

1. Compile the first MySQL instance

cd bld3306

# -DMYSQL_TCP_PORT=3306 为指定端口号
# /usr/local/mysql3306 为 mysql3306 安装目录
cmake3 -DMYSQL_TCP_PORT=3306 -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DCMAKE_INSTALL_PREFIX=/usr/local/mysql3306 -DWITH_BOOST=../boost ..

make -j4

2. Install the first MySQL instance

make install

3. Configure the first MySQL configuration file

vim /etc/my3306.cnf

# 文件内容如下


[client] 
socket=/usr/local/mysql3306/data/mysql.sock 
 
[mysqld] 
datadir=/usr/local/mysql3306/data 
socket=/usr/local/mysql3306/data/mysql.sock 
 
#Disabling symbolic-links is recommended to prevent assorted security risks 
symbolic-links=0 
log-error=/usr/local/mysql3306/data/mysqld.log 
pid-file=/usr/local/mysql3306/data/mysqld.pid 
server-id=1
port=3306

#是否只读,1代表只读,0代表读写
read-only=0

#忽略的数据,指不需要同步的数据库
#binlog-ignore-db=mysql
#指定同步的数据库
#binlog-do-db=db01

4. Initialize the first MySQL instance

# 进入安装目录
cd /usr/local/mysql3306

# 初始化
bin/mysqld --defaults-file=/etc/my3306.cnf --initialize --user=mysql3306 --port=3306

 5. Start the first MySQL instance

/usr/local/mysql3306/bin/mysqld_safe --defaults-file=/etc/my3306.cnf --user=mysql3306 --port=3306 &

Special note 1: When starting mysql, the specified configuration file must be displayed, namely --defaults-file=/etc/my3306.cnf, otherwise various errors will be reported! ! !

6. Set the root password of the first MySQL instance

# 执行下述命令并使用初始密码进入 mysql
/usr/local/mysql3306/bin/mysql -h127.0.0.1 -uroot -p -P3306


ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

Special note 2: Be sure to write a complete command when entering mysql, ie /usr/local/mysql3306/bin/mysql -h127.0.0.1 -uroot -p -P3306 Otherwise multiple instances cannot be started, which is very important! very important! very important! If you do not specify h127.0.0.1, it will use mysql.sock to enter by default when entering mysql. In this way, multiple instances actually enter one mysql! ! !

7. Complete the deployment of the first MySQL instance

4. Deploy the second mysql --- 3307

1. Compile the second MySQL instance

cd bld3307

# -DMYSQL_TCP_PORT=3307 为指定端口号
# /usr/local/mysql3307 为 mysql3307 安装目录
cmake3 -DMYSQL_TCP_PORT=3307 -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DCMAKE_INSTALL_PREFIX=/usr/local/mysql3307 -DWITH_BOOST=../boost ..

make -j4

2. Install the second MySQL instance

make install

3. Configure the second MySQL configuration file

vim /etc/my3307.cnf

# 文件内容如下


[client] 
socket=/usr/local/mysql3307/data/mysql.sock 
 
[mysqld] 
datadir=/usr/local/mysql3307/data 
socket=/usr/local/mysql3307/data/mysql.sock 
 
#Disabling symbolic-links is recommended to prevent assorted security risks 
symbolic-links=0 
log-error=/usr/local/mysql3307/data/mysqld.log 
pid-file=/usr/local/mysql3307/data/mysqld.pid 
server-id=2
port=3307

#是否只读,1代表只读,0代表读写
read-only=0

#忽略的数据,指不需要同步的数据库
#binlog-ignore-db=mysql
#指定同步的数据库
#binlog-do-db=db01

4. Initialize the second MySQL instance

# 进入安装目录
cd /usr/local/mysql3307

# 初始化
/usr/local/mysql3307/bin/mysqld --defaults-file=/etc/my3307.cnf --initialize --user=mysql3307 --port=3307

5. Start the second MySQL instance

/usr/local/mysql3307/bin/mysqld_safe --defaults-file=/etc/my3307.cnf --user=mysql3307 --port=3307 &

Special note 3: When starting mysql, the specified configuration file must be displayed, namely --defaults-file=/etc/my3306.cnf, otherwise various errors will be reported! ! ! 

6. Set the root password for the second MySQL instance

# 执行下述命令并使用初始密码进入 mysql
/usr/local/mysql3307/bin/mysql -h127.0.0.1 -uroot -p -P3307


ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

Special note 2: Be sure to write a complete command when entering mysql, ie /usr/local/mysql3306/bin/mysql -h127.0.0.1 -uroot -p -P3306 Otherwise multiple instances cannot be started, which is very important! very important! very important! If you do not specify h127.0.0.1, it will use mysql.sock to enter by default when entering mysql. In this way, multiple instances actually enter one mysql! ! ! 

Explanation: What is the difference between specifying ip and not specifying ip when logging in to mysql?

(1) -h127.0.0.1)In the case of specifying IP (:

  • The parameter is used -hto specify the host address to be connected, here 127.0.0.1is the local loopback address (localhost).

  • This means that you are connecting directly to the MySQL DB instance located on your local machine, regardless of other network configuration or middleware, you will be connecting to the specified IP address.

(2) When no IP is specified:

  • If you do not -hspecify a host address with the parameter, the MySQL client will try to connect to the local host by default.

  • This can cause MySQL clients to attempt to connect to the MySQL server through the default network interface in cases with multiple network interfaces or middleware configurations. This can cause you to connect to the wrong MySQL instance or have connection problems, especially if there are multiple instances running.

Therefore, it is recommended to explicitly specify the address of the host to be connected when using the command line to connect to MySQL, so as to ensure that the correct MySQL instance is connected. If you are connecting to a port-specific MySQL instance, and you know exactly which IP address it is running on, it is best to use the -hparameter to specify the target IP address.

7. Complete the deployment of the second MySQL instance

5. Special instructions

The above deployment of two mysql --- 3306, 3307 on one machine, install mysql twice, in fact, only install mysql once, and then create two new configuration files, and display the specified configuration file at startup can also achieve the same purpose ; This method is equivalent to opening two independent processes, and the effect is the same as above! ! !

After several days of tossing, it is finally over! !

Guess you like

Origin blog.csdn.net/weixin_47156401/article/details/132135356