[Detailed explanation of Mysql deployment on Linux, personal test]

1. Preparation

1. Download the mysql installation package

Official website address: https://downloads.mysql.com/archives/community/
Insert image description here

2. Uninstall MariaDB

MariaDB will conflict with mysql, so uninstall it if possible.
①: Query MariaDB

rpm -qa | grep mariadb

Insert image description here
②: Delete file

yum -y remove mariadb-libs-5.5.68-1.el7.x86_64
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64

③: Ensure correctness and check again

rpm -qa | grep mariadb

3. Add mysql user group and mysql user

①: Check whether the mysql user and group exist. If the following command is not executed, if so, whether to use or create individual user groups according to the situation.

cat /etc/group |grep mysql 

②: Create a group

groupadd mysql

③: Create a user and add the user to the group mysql. The -r here means that the user is an internal user and no external login is allowed.

useradd -r -g mysql mysql

④: Set password

passwd mysql 

⑤: Check whether the creation is successful

groups mysql

2. Install Mysql

1. Upload the file and determine the installation path

①: Create the data folder, and the /data/software folder stores the software, then decompress it, rename the decompressed file and move it to the /opt/mysql folder. (You can also place it directly in the folder you like, unzip it and rename it)

## 切换到根目录
cd /
## 创建data文件夹
mkdir data
## 进入data创建software文件夹
cd /data
mkdir software

My installation directory: /opt/mysql

②: Unzip the file and rename it

1):更换路径并且重命名
## 切换路径
cd /data/software/
## 解压
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
## 重命名,调整路径到/opt/mysql
mv /data/software/mysql-5.7.25-linux-glibc2.12-x86_64 /opt/mysql5.7.25
2):文件上传到所在路径,解压并且重命名
## 切换路径
cd /opt
## 解压
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
## 重命名
mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql5.7.25

③: Delete the default configuration, rm -rf is not recommended (no is best)

rm /etc/my.cnf
y

Insert image description here

3. Make Mysql settings

1. Enter the bin directory and initialize Mysql

## 进入安装目录
cd /opt/mysql5.7.25/bin
## 初始化Mysql(初始化Mysql数据目录:mysql_install_db!!指定mysql安装目录basedir!!指定数据存储路径datadir!!指定源码目录路径srcdir)
 ./mysqld --initialize --user=mysql --datadir=/opt/mysql5.7.25/data --basedir=/opt/mysql5.7.25

2. Remember the password after initialization! !

[Note] A temporary password is generated for root@localhost: m,Vv7#2or8EP

3. Enter the etc directory, create and modify the my.cnf file

## 进入etc目录
cd /etc
## 创建my.cnf文件
> my.cnf
## 授权
chmod 775 /etc/my.cnf
chmod -R 775 /etc/my.cnf

4. yum install libaio

yum install libaio

5. Enter the mysql5.7/support-files/ directory and modify the path address.

## 进入地址
cd /opt/mysql5.7.25/support-files
## 如下图修改地址路径

6. Try to start

## 先启动试一下(success ,正常)
/opt/mysql5.7.25/support-files/mysql.server start 

结果报错:
Starting MySQL.2023-07-30T09:16:08.002171Z mysqld_safe error: log-error set to ‘/opt/mysql5.7.25/logs/mysqld.log’, however file don’t exists. Create writable for user ‘mysql’.
ERROR! The server quit without updating PID file (/opt/mysql5.7.25/data/localhost.localdomain.pid).

解决:

cd /opt/mysql5.7.25
mkdir logs
echo "" > /opt/mysql5.7.25/logs/mysqld.log
chown -R mysql:mysql /opt/mysql5.7.25/logs/mysqld.log

仍然报错:
Starting MySQL.2023-07-30T09:26:51.374870Z mysqld_safe Directory ‘/var/lib/mysql’ for UNIX socket file don’t exists.
ERROR! The server quit without updating PID file (/opt/mysql5.7.25/data/localhost.localdomain.pid).

解决:

mkdir /var/lib/mysql
chown -R mysql.mysql /var/lib/mysql/

The error is still reported:
ERROR! The server quit without updating PID file (/opt/mysql5.7.25/data/localhost.localdomain.pid).
Solution:
The reason is as shown below. The path to the failure log is added to the my.cnf file. I don’t know why. , just remove the restart! !
Insert image description here
Insert image description here

7. Configure environment variables and upload the "mysql.sh" file

## 切换路径
cd /etc/profile.d
## 上传文件
## 环境变量立即生效
source /etc/profile

Insert image description here

8. Restart MySQL

## 重启Mysql
/opt/mysql5.7.25/support-files/mysql.server restart
## 登陆Mysql
mysql -u root -p
## 输入刚才记录的密码
m,Vv7#2or8EP

As a result, there was another problem with logging in.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Reason for the error: The sock link file for Mysql is not generated.
First, you need to understand the role of the mysql.sock file:
connect to localhost Usually through a Unix domain socket file, usually /tmp/mysql.sock.
If the socket file is deleted, local clients cannot connect. This may happen if your system runs a cron job that deletes temporary files in /tmp.
If you are unable to connect because the socket file is missing, you can simply recreate it by restarting the server. Because the server recreates it on startup.

## 先查询下在哪
find / -name mysql.sock


Solution: Mysql.sock is not only added to mysqlid in my.cnf, but also socket=/var/lib/mysql/mysql.sock is added to the client.
Insert image description here

问题2、ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)

Okay, I entered the wrong password! What a sin!

The errors are reported endlessly and endlessly! ! Next modify permissions

4. Permissions and firewall settings

1. Permission settings, password changes

## 设置密码
set PASSWORD = PASSWORD('123456');
## 刷新
flush privileges;
## 切换数据库
use mysql;
## 更改权限
update user set host='%' where user='root';
## 赋权:给root权限,所有IP 都可以连接:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
## 退出,完成
exit

2. Restart and check the status

## 重启
/opt/mysql5.7.25/support-files/mysql.server restart
## 查看mysql的状态
ps -ef|grep mysql

3. Firewall

## 查看防火墙是否已开放3306端口
firewall-cmd --query-port=3306/tcp
## 设置3306端口为永久开放
firewall-cmd --add-port=3306/tcp --permanent
## 查看firewalld状态,发现当前是dead状态,即防火墙未开启
systemctl status firewalld
## 关闭防火墙
systemctl stop firewalld
## 重启防火墙(设置了新的端口记得先关闭,再重启)
systemctl status firewalld

Problem: The above operations are performed, but when an error is reported using the visualization tool, the server can be connected.

2003-Can’t connect to Mysql server on ‘xxx’ (10060 “Unknown error”)
解决:

## 进入mysql,执行权限授权
mysql -uroot -p
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
## 刷新生效
FLUSH PRIVILEGES;
## 打开防火墙
firewall-cmd --zone=public --add-port=3306/tcp --permanent
## 结果,已经打开
[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
Warning: ALREADY_ENABLED: 3306:tcp
## 重启防火墙并查看是否生效
firewall-cmd --reload		#重启firewall
firewall-cmd --list-ports	#查看已经开放的端口

Insert image description here

Guess you like

Origin blog.csdn.net/m0_49762804/article/details/132010952