当前位置: 首页 > news >正文

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

目录

    • 一、检查系统是否安装其他版本Mariadb数据库
    • 二、安装 MySQL
    • 三、配置 MySQL
    • 四、修改默认存储路径
    • 五、开放防火墙端口
    • 六、数据备份
    • 七、生产环境优化
    • 八、常用命令

一、检查系统是否安装其他版本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_installationSecuring 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=0log-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服务当前在启动状态
http://www.xdnf.cn/news/252397.html

相关文章:

  • C++ - 数据容器之 list(创建与初始化、元素访问、容量判断、元素遍历、添加元素、删除元素)
  • 父子组件双向绑定
  • ElasticSearch深入解析(八):索引设置、索引别名、索引模板
  • Windows配置grpc
  • 红米Note9 4G版拆开后盖操作细节
  • 51c嵌入式~电路~合集4
  • Ubuntu搭建Conda+Python开发环境
  • 【AI面试准备】Azure DevOps沙箱实验全流程详解
  • 【KWDB 创作者计划】利用KWDB解决工业物联网场景中的海量数据管理难题的思考
  • 处方流转平台权限控制模块设计(基于RBAC模型)
  • Midjourney 绘画 + AI 配音:组合玩法打造爆款短视频!
  • Notion 系列(一):页面与数据库的结构化实践
  • [Android] 网易爆米花TV 2.0.0.0429(原网易Filmly,支持多网盘的TV版、电脑版带海报墙播放器)
  • 【Java IO流】IO流详解
  • 【Linux】PetaLinux开发
  • 一个读写excel的简单程序(golang)
  • 云原生后端架构的优势与最佳实践
  • Unity 与 Lua 交互详解
  • docker 官方:在 alpine 上安装 python 的方法
  • Sphinx 文档图片点击放大
  • 内部类(3):匿名内部类
  • 藏文情感分析器入门学习实践
  • Electron学习+打包
  • 【Java函数式编程-58.2】深入理解Java中的Function函数式接口
  • iO(不可区分混淆)是Web3隐私的圣杯?
  • xshell 左边的会话管理器不见怎么办?
  • d202552-sql
  • 深入解析MapReduce:大数据处理的经典范式
  • 基于建造者模式的信号量与理解建造者模式
  • Linux架构篇、第一章_03安装部署nginx