openEuler 22.03 安装 Mysql 5.7,RPM 在线安装

一、检查系统是否安装其他版本Mariadb数据库

# 查看已安装的 Mariadb 数据库版本
[root@openeuler ~]# rpm -qa|grep -i mariadb
# 卸载已安装的 Mariadb 数据库
[root@openeuler ~]# rpm -qa|grep mariadb|xargs rpm -e --nodeps

二、安装 MySQL

2.1 安装 MySQL

# 创建用于存放 MySQL Yum 源配置文件的目录
[root@openeuler ~]# sudo mkdir -p /etc/yum.repos.d/mysql

# 下载 MySQL 官方的 Yum 源配置文件
[root@openeuler ~]# sudo wget -P /etc/yum.repos.d/mysql https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

# 安装下载的 repo 文件
[root@openeuler ~]# sudo rpm -ivh /etc/yum.repos.d/mysql/mysql57-community-release-el7-11.noarch.rpm

# 查看可用的 mysql 安装文件
[root@openeuler ~]# dnf repolist all | grep mysql

# 非常重要:防止GPG key报错,执行这个命令
[root@openeuler ~]# rm /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
rm:是否删除普通文件 '/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql'[root@openeuler ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

# 安装 MySQL 服务
[root@openeuler ~]# sudo dnf install -y mysql-community-server

# 检查mysql是否安装成功
[root@openeuler ~]# rpm -qa | grep mysql

2.2 启动服务

# 启动服务
[root@openeuler ~]# sudo systemctl start mysqld
# 查看服务状态
[root@openeuler ~]# sudo systemctl status mysqld
# 设置开机自启
[root@openeuler ~]# sudo systemctl enable mysqld
# 获取初始临时密码,为root用户随机生成了一个密码
[root@openeuler ~]# sudo grep 'temporary password' /var/log/mysqld.log
2025-04-29T21:23:11.322498Z 1 [Note] A temporary password is generated for root@localhost: XXXXXX(临时密码)

2.3 运行安全配置向导
设置内容如下:
1.为root用户设置密码
2.删除匿名账号
3.取消root用户远程登录
4.删除test库和对test库的访问权限
5.刷新授权表使修改生效

[root@openeuler ~]# sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: ##<– 输入mysqld.log里的临时密码

The existing password for the user account root has expired. Please set a new password.

New password: ##<– 输入新密码。要求:大小写字母、数字、特殊字符组合

Re-enter new password: ##<– 输入新密码。要求:大小写字母、数字、特殊字符组合
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y ##<– 选择是

New password: ##<– 输入新密码。要求:大小写字母、数字、特殊字符组合

Re-enter new password: ##<– 输入新密码。要求:大小写字母、数字、特殊字符组合

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y ##<– 是否删除匿名用户,生产环境建议删除,所以直接回车
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N ##<– 是否禁止root远程登录,根据需求选择,建议禁止

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N ##<– 是否删除test数据库,直接回车

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : ##<– 是否重新加载权限表,直接回车

 ... skipping.
All done! 

2.4 配置 my.cnf

# 备份my.cnf文件
[root@openeuler ~]# sudo cp -a /etc/my.cnf /etc/my_back.cnf
# 编辑my.cnf文件
[root@openeuler ~]# sudo vi /etc/my.cnf

# 修改以下内容:
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

#############      分割线  从下面开始修改      #############
# 默认端口3306
# 注意:此处修改后,一定要更新 SELinux 端口策略
port=3366

# 设置编码
# character-set-server=utf8
# collation-server=utf8_general_ci
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

# 1.默认采用InnoDB存储引擎
default-storage-engine=INNODB

# 2.设置大小写敏感
lower_case_table_names=1

# 3.sql_mode定义了支持的sql语法、数据校验等
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

# 最大并行连接数
max_connections=100000

[mysql]
# default-character-set = utf8
default-character-set = utf8mb4

[mysql.server]
# default-character-set = utf8
default-character-set = utf8mb4

[mysqld_safe]
# default-character-set = utf8
default-character-set = utf8mb4

[client]
# default-character-set = utf8
default-character-set = utf8mb4

2.5 重启服务

# 如果/etc/my.cnf修改了默认端口,此处一定要更新 SELinux 端口策略
[root@openeuler ~]# sudo semanage port -a -t mysqld_port_t -p tcp 3366
# 重启
[root@openeuler ~]# sudo systemctl daemon-reload
[root@openeuler ~]# sudo systemctl restart mysqld

三、配置 MySQL

# 登录 MySQL
[root@openeuler ~]# mysql -u root -p
Enter password:     ## 在【运行安全配置向导】设置的密码

# 重置密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '你的新密码';
# 授权远程访问权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的新密码' WITH GRANT OPTION;
# 刷新策略
mysql> FLUSH PRIVILEGES;

#修改密码长度限制策略
mysql> set global validate_password_policy=0;   #--表示将密码安全等级设置为low
mysql> set global validate_password_length=1;   #--表示将密码长度设置为最小6位
mysql> FLUSH PRIVILEGES;

# 查看服务编码UTF8
mysql> SHOW VARIABLES LIKE 'character%';
# 查看最大连接数
mysql> SHOW VARIABLES LIKE 'max_connections';
# 查看存储路径
mysql> SHOW VARIABLES LIKE '%datadir%';

# 退出
mysql> exit

四、修改默认存储路径

修改 Mysql 5.7 默认数据库存储路径

五、开放防火墙端口

# 查询端口是否开放
[root@openeuler ~]# sudo firewall-cmd --query-port=3306/tcp
[root@openeuler ~]# sudo firewall-cmd --query-port=3366/tcp
# 开放端口(TCP协议)
[root@openeuler ~]# sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@openeuler ~]# sudo firewall-cmd --zone=public --add-port=3366/tcp --permanent
# 重新加载防火墙规则
[root@openeuler ~]# sudo firewall-cmd --reload
# 验证端口是否开放
[root@openeuler ~]# sudo firewall-cmd --zone=public --list-ports
# 查看所有端口
[root@centosServer ~]# sudo netstat -nlpt

六、数据备份

Linux 环境下 Mysql 5.7 数据定期备份

七、生产环境优化

生产环境优化 Mysql 5.7

八、常用命令

# 启动服务
sudo systemctl start mysqld
# 查看服务状态
sudo systemctl status mysqld
# 重启服务
sudo systemctl restart mysqld
# 停止服务
sudo systemctl stop mysqld

#开启开机启动
sudo systemctl enable mysqld
#关闭开机启动
sudo systemctl disable mysqld
#查看开机启动
sudo systemctl list-unit-files | grep mysqld

查看版本:mysql -V

查看进程:ps -ef | grep mysqld
如果有mysqld_safe和mysqld两个进程,说明MySQL服务当前在启动状态
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值