Alibaba Cloud installation and remote connection to mysql8.0 database

Table of contents

Install MySQL8.0 (detailed version) on the cloud server:

The solution to the two problems encountered during the installation process

Unable to get file status (stat) for 'support-files/mysql.server': No such file or directory

Solve the problem that the remote connection cannot be made, and the error Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


Install MySQL8.0 (detailed version) on the cloud server:

1. Download the Linux installation package: MySQL :: Download MySQL Community Server (Archived Versions)

2. Install libaio dependencies

libaiois a library for asynchronous input/output operations that provides support for asynchronous I/O interfaces. It is one of the dependencies required by many applications and database engines. When installing a database engine such as MySQL, you usually need to install it libaioas its dependency, because these database engines usually use asynchronous I/O operations to improve the efficiency and performance of data reading and writing.

yum install libaio

 3. Put the downloaded mysql compressed package into the /usr/local/ directory. I use finalshell to connect to the cloud server, so I can directly upload the compressed package just downloaded to the cloud server:

cd /usr/local/

4. Unzip MySQL

tar -xvf mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz

5. Rename the folder

mv mysql-8.0.27-linux-glibc2.12-x86_64 mysql8

6. Create two new folders under the just renamed mysql8 folder

mkdir data
mkdir tmp

 7. Create a user group, user and password. It may appear that the mysql user already exists. The problem is not big, so don’t worry about it.

groupadd mysql
useradd -g mysql mysql

8. Authorize files for mysql users

chown -R mysql.mysql /usr/local/mysql8/

9. Edit my.cnf file vi /etc/my.cnf

vi /etc/my.cnf 

Configuration: Just copy it directly

[mysqld]
basedir = /usr/local/mysql8
datadir = /usr/local/mysql8/data
port = 3306
socket = /usr/local/mysql8/tmp/mysql.sock

#必填项
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4
socket = /usr/local/mysql8/tmp/mysql.sock

10. Switch to the bin directory of mysql8 to initialize

cd bin            或者是  sd /usr/local/mysql8/bin

./mysqld --initialize --user=mysql   初始化mysql

After initialization, a random password will appear, which is used for login and needs to be recorded for later modification of the password.

11. Add mysqld service to the system

First switch to the support-files directory:

先查询刚刚解压包中mysql.server文件的位置:
find / -name mysql.server

 To copy files:

cp /usr/local/mysql8/support-files/mysql.server /etc/init.d/mysql.server

Authorize the file and add the mysql service:

chmod +x /etc/init.d/mysql.server

chkconfig --add mysql.server

Check whether the addition is successful:

chkconfig --list

 12. Add the mysql command to the service

ln -sf /usr/local/mysql8/bin/mysql /usr/bin

13. Start the service

service mysql.server start

14. View the startup status of MySQL

service mysql.server status

15. Login to MySQL

mysql -uroot -p   

密码是刚刚生成的随机密码

Change the root password:

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

flush privileges;

16. Switch to the mysql library for remote connection permission settings:

use mysql;

select host,user,plugin from user;

update user set host = '%' where user ='root';
flush privileges;

select host,user,plugin from user;

17. Configure a security group on the Alibaba Cloud server and open port 3306

 18. Open port 3306 in the firewall

1、检查防火墙状态:如果防火墙处于活动状态,将显示相关信息,包括防火墙是否正在运行。
sudo systemctl status firewalld

2、查看防火墙规则:显示当前防火墙规则的详细列表,包括允许和拒绝的规则。
sudo firewall-cmd --list-all

3、允许数据库端口:如果发现防火墙规则中没有允许数据库端口的规则,可以添加相应的规则:
sudo firewall-cmd --zone=public --add-port=端口号/tcp --permanent

sudo firewall-cmd --reload

19. Remote connection test:

Create a database for testing:

create database reggie;

use reggie;

CREATE TABLE `user` (
  `id` bigint NOT NULL COMMENT '主键',
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '姓名',
  `phone` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '手机号',
  `sex` varchar(2) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '性别',
  `id_number` varchar(18) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '身份证号',
  `avatar` varchar(500) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '头像',
  `status` int DEFAULT '0' COMMENT '状态 0:禁用,1:正常',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC COMMENT='用户信息';

INSERT INTO reggie.`user`
(id, name, phone, sex, id_number, avatar, status)
VALUES(1526738180875788289, '小白', '115874605588', NULL, NULL, NULL, 1);

Use dbeaver to connect remotely:

 Test the connection, the connection is successful!

The solution to the two problems encountered during the installation process

Unable to get file status (stat) for 'support-files/mysql.server': No such file or directory

Question 1: When executing cp support-files/mysql.server /etc/init.d/mysql.server to name and copy mysql.server to the specified file, it will appear: Unable to obtain the file status of 'support-files/mysql.server' (stat): No such file or directory

Solution: Use  find / -name mysql.server to find the path of the mysql.server file, replace the path of the above command after finding it, and re-execute the above cp command.

The second problem: the MySQL in this server cannot be connected remotely. After various investigations below, it is found that the cloud server has not opened the port 3306.

使用Dbeaver进行远程连接报错:Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. connect timed out  connect timed out

After querying the information on the Internet, there are various opinions, such as: MySQL on the server is not started, the port in the configuration file is wrongly written, the security group of Alibaba Cloud does not allow 3306 connection permissions, and cj (com .mysql.cj.jdbc.Driver). If the installation is performed according to the above reference article, it is not because of these reasons that the remote connection cannot be made. After various investigations, it is found that the server's firewall has not opened the port 3306.

Solution:

1、检查防火墙状态:如果防火墙处于活动状态,将显示相关信息,包括防火墙是否正在运行。
sudo systemctl status firewalld

2、查看防火墙规则:显示当前防火墙规则的详细列表,包括允许和拒绝的规则。
sudo firewall-cmd --list-all

3、允许数据库端口:如果发现防火墙规则中没有允许数据库端口的规则,可以添加相应的规则:
sudo firewall-cmd --zone=public --add-port=端口号/tcp --permanent

sudo firewall-cmd --reload

4、重新测试连接

Solve the problem that the remote connection cannot be made, and the error Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

The idea to solve the remote connection failure: the premise is that the configuration file is correct, the security group of the Alibaba Cloud server for data entry is also configured, and the database also allows remote connection, but the remote connection to the database is not yet possible.

1. Check the network connection problem: Make sure the network connection is normal and can communicate with the database server.

ping 目标主机ip地址   看没有字节可以接受到,如下

56(84) bytes of data.
64 bytes from 目标主机的 IP 地址: icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from 目标主机的 IP 地址: icmp_seq=2 ttl=64 time=0.056 ms
...

2. Check whether the database server is running successfully or not:

ps -ef | grep mysqld  检查MySQL数据库是否在运行

netstat -tuln | grep 3306  检查数据库监听的端口

 3. Check the configuration of the database server to make sure it is listening on the correct port and allowing remote connections:

telnet 目标主机 端口  

正确的回应应该如下:
Trying 目标主机的 IP 地址...
Connected to 目标主机的 IP 地址.
Escape character is '^]'.
如果无法连接到该端口,则表示该端口可能未被监听或存在连接问题。

4. Firewall issues: If the database server is behind a firewall, please make sure that the firewall is properly configured to allow remote connections (using the CentOS operating system). Usually MySQL defaults to port 3306.

1、检查防火墙状态:如果防火墙处于活动状态,将显示相关信息,包括防火墙是否正在运行。
sudo systemctl status firewalld

2、查看防火墙规则:显示当前防火墙规则的详细列表,包括允许和拒绝的规则。
sudo firewall-cmd --list-all

3、允许数据库端口:如果发现防火墙规则中没有允许数据库端口的规则,可以添加相应的规则:
sudo firewall-cmd --zone=public --add-port=端口号/tcp --permanent

sudo firewall-cmd --reload

4、重新测试连接

The above four are the process of checking and troubleshooting that I can't connect remotely. Finally, it was found that the firewall did not open port 3306, so after adding it, you can connect remotely. Hope it helps you!

Reference article: There are two problems encountered in the reference article, which have been solved in this blog article:

Install mysql8 on Alibaba Cloud ECS server

Guess you like

Origin blog.csdn.net/weixin_53142722/article/details/131140187