AlmaLinux (Centos8.5)安装并使用 MySQL 8.0 数据库

本文详细介绍了如何在AlmaLinux 8.5系统上安装并配置MySQL 8.0,包括检查并卸载旧版本、添加官方yum repository、设置root密码、启用额外连接和管理端口,以及使用Navicat远程登录。重点讲解了新版本的特性,如管理端口的使用和权限管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. 机器准备

#一台虚拟机
hostname:myhost
ip:ip_address

2. 系统环境

[root@myhost ~]# cat /etc/redhat-release 
AlmaLinux release 8.5 (Arctic Sphynx) 
[root@myhost ~]# firewall-cmd --state							 #查看防火墙的状态
running
[root@myhost ~]# firewall-cmd --list-ports                       #查看防火墙已经开放的端口
22/tcp 
[root@myhost ~]# firewall-cmd --list-services                    #查看防火墙开放的服务
cockpit dhcpv6-client ssh
#假设设置mysql的服务端口为3306,允许3306/tcp端口访问 
[root@myhost ~]# firewall-cmd --zone=public --permanent --add-port=3306/tcp
success
[root@myhost ~]# firewall-cmd --reload                                       	#重新加载防火墙配置
success 
[root@myhost ~]# setenforce 0  &&    getenforce
Permissive
[root@myhost ~]# sed -i  "s/SELINUX=enforcing/SELINUX=disabled/g"  /etc/sysconfig/selinux 
[root@myhost ~]# cat /etc/sysconfig/selinux 

3. 安装配置使用mysql8.0

3.1 检查是否有安装mysql数据库(如果有mysql或者mariadb数据库,则卸载)
[root@myhost ~]# rpm -qa |grep mysql
[root@myhost ~]# rpm -qa | grep mariadb
[root@myhost ~]# ll /etc/my.cnf 
ls: 无法访问'/etc/my.cnf': No such file or directory

#如果有mysql或者mariadb数据库,则卸载
[root@myhost ~]# yum -y  remove `rpm -qa | grep mysql` 或者  yum -y  remove `rpm -qa | grep mariadb`  
[root@myhost ~]# ll  /etc/my.cnf
ls: 无法访问'/etc/my.cnf': No such file or directory
[root@myhost ~]# rpm -qa |grep mysql 或者 rpm -qa | grep mariadb
3.2 下载并安装mysql官方的 yum repository
[root@myhost ~]# cd /home/tools/
[root@myhost tools]# wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
[root@myhost tools]# ll
总用量 28
-rw-r--r--. 1 root root 25820 4月  18 2018 mysql80-community-release-el7-1.noarch.rpm
3.3 rpm安装生成所需要的repo文件
[root@myhost tools]# ll /etc/yum.repos.d/
总用量 20
-rw-r--r--. 1 root root  941 11月 10 10:55 almalinux-ha.repo
-rw-r--r--. 1 root root  883 11月 10 10:55 almalinux-plus.repo
-rw-r--r--. 1 root root  961 11月 10 10:55 almalinux-powertools.repo
-rw-r--r--. 1 root root 2660 11月 10 10:55 almalinux.repo
-rw-r--r--. 1 root root 1022 11月 10 10:55 almalinux-resilientstorage.repo

[root@myhost tools]# rpm -ivh mysql80-community-release-el7-1.noarch.rpm
#执行完成后会在/etc/yum.repos.d/目录下生成两个repo文件: mysql-community.repo 和 mysql-community-source.repo

[root@myhost tools]# ll /etc/yum.repos.d/                               
总用量 28
-rw-r--r--. 1 root root  941 11月 10 10:55 almalinux-ha.repo
-rw-r--r--. 1 root root  883 11月 10 10:55 almalinux-plus.repo
-rw-r--r--. 1 root root  961 11月 10 10:55 almalinux-powertools.repo
-rw-r--r--. 1 root root 2660 11月 10 10:55 almalinux.repo
-rw-r--r--. 1 root root 1022 11月 10 10:55 almalinux-resilientstorage.repo
-rw-r--r--. 1 root root 1864 2月  22 2018 mysql-community.repo   ###新生成的repo文件###
-rw-r--r--. 1 root root 1885 2月  22 2018 mysql-community-source.repo    ###新生成的repo文件###

3.4 正式安装mysql服务器
[root@myhost tools]# yum install -y mysql-server

[root@myhost tools]#  rpm -qa |grep mysql
mysql-8.0.26-1.module_el8.4.0+2532+b8928c02.x86_64
mysql-common-8.0.26-1.module_el8.4.0+2532+b8928c02.x86_64
mysql-server-8.0.26-1.module_el8.4.0+2532+b8928c02.x86_64
mysql80-community-release-el7-1.noarch
mysql-errmsg-8.0.26-1.module_el8.4.0+2532+b8928c02.x86_64

至此,mysql服务器安装完毕!!!
3.5 查看mysql 8.0 数据库主配置文件
[root@myhost tools]# ll /etc/my.cnf
-rw-r--r--. 1 root root 202 12月  3 2020 /etc/my.cnf
[root@myhost tools]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d   ##这里引入MySQL 8.0 数据库各个详细配置文件##

[root@myhost tools]# ll /etc/my.cnf.d
总用量 12
-rw-r--r--. 1 root root 295 12月  3 2020 client.cnf
-rw-r--r--. 1 root root 565 8月  24 2021 mysql-default-authentication-plugin.cnf
-rw-r--r--. 1 root root 612 9月  24 2021 mysql-server.cnf
  • 服务端配置文件
[root@myhost tools]# cat /etc/my.cnf.d/mysql-server.cnf
#
# This group are read by MySQL server.
# Use it for options that only the server (but not clients) should see
#
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html

# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
  • 客户端配置文件
[root@myhost tools]# cat /etc/my.cnf.d/client.cnf 
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
3.6 启动mysql
### 启动mysql
[root@myhost tools]# systemctl start mysqld.service 

### 查看MySQL运行状态
[root@myhost tools]# systemctl status mysqld.service     
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-05-05 03:07:44 EDT; 10s ago
  Process: 53994 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 53862 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 53837 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 53947 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 23508)
   Memory: 460.1M
   CGroup: /system.slice/mysqld.service
           └─53947 /usr/libexec/mysqld --basedir=/usr

5月 05 03:07:35 myhost systemd[1]: Starting MySQL 8.0 database server...
5月 05 03:07:35 myhost mysql-prepare-db-dir[53862]: Initializing MySQL database
5月 05 03:07:44 myhost systemd[1]: Started MySQL 8.0 database server.

### 将MySQL加入开机自启动
[root@myhost tools]# systemctl enable mysqld.service           
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
3.7 查看mysql的初始登录密码

当MySQL开始正常运行时,要想进入MySQL还得先找出此时root用户的密码,root用户的密码可以在MySQL日志文件中找出,前面安装MySQL时,服务端配置文件里面显示了MySQL的日志文件所在路径:log-error=/var/log/mysql/mysqld.log

[root@myhost tools]#  egrep -v "^#|^$"  /etc/my.cnf.d/mysql-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid

[root@myhost tools]# grep "password" /var/log/mysql/mysqld.log
2022-05-05T07:07:39.174205Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

根据日志中显示,此次安装mysql时没有设置root用户密码(即可以使用空密码进行登录)

3.8 登录mysql数据库
[root@myhost tools]# mysql -uroot -p
Enter password: #空密码#
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.26    |
+-----------+
1 row in set (0.00 sec)

mysql&g
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值