Llinux安装MySQL教程

本文环境:CentOS7、mysql-8.0.26

使用FinalShell或者XShell连接Linux服务器,然后把网上下的MySQL的tar包传进去,我用的是mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar

进入服务器以后创建一个临时文件夹用来解压文件夹

shell 复制代码
cd /tmp

mkdir /tempData

cd /tempData

# 把tar包传到/tmp/tempData以后,在这里创建一个文件夹然后解压tar包
mkdir mysql
# 下面的tar包记得替换成自己的,这里会把tar包解压到刚创建的mysql文件夹里
tar -xvf mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar -C mysql

cd mysql
# 现在的mysql文件夹里有一堆rpm文件,这里需要按照一定的顺序安装才行
rpm -ivh mysql-community-common-8.0.26-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm
# 下面这行执行如果报错,需要执行【rpm -e mariadb-libs --nodeps】命令然后再执行下面这行命令
rpm -ivh mysql-community-libs-8.0.26-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-compat-8.0.26-1.el7.x86_64.rpm
# 执行下面的命令,安装openssl-devel,但是现在的CentOS7仓库据说是不提供支持了,可能会报错,报错信息和解决方案在文章下面,把仓库源换成阿里的就行(其他能用的仓库也可以)
yum install openssl-devel

rpm -ivh  mysql-community-devel-8.0.26-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-8.0.26-1.el7.x86_64.rpm

rpm -ivh  mysql-community-server-8.0.26-1.el7.x86_64.rpm

# 总结一下安装顺序
common -> client-plugins -> libs -> libs-compat -> devel -> client -> server

# 其中安装libs可能会报依赖错误,这时候执行一次【rpm -e mariadb-libs --nodeps】再安装libs就好
# 另外安装devel之前要先安装openssl-devel

# 上面的rpm安装完毕没问题了就可以启动mysql了
systemctl start mysqld
# 在windows安装MySQL的时候,大家可能碰到过设置密码的时候,但是linux安装rpm的时候是没有设置密码这个流程的,系统会随机生成一个文件,我们通过下列命令查找生成的随机密码
grep 'temporary password' /var/log/mysqld.log

# 这时候我这边显示这样的信息
2025-03-19T15:23:19.674016Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: =qx_Qtd#%3c7

# 密码就是=qx_Qtd#%3c7
# 知道密码以后可以连接数据库了
mysql -u root -p=qx_Qtd#%3c7

# 如果此时你尝试创建数据库
create database test;
# 你会直接收到一个错误提示:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
# 解决方法很简单,就是把初始化的密码改一下,然后就可以正常使用mysql了
# 但是mysql对密码有校验规则和长度限制,如果你想要使用简单的密码比如123456,那就先降低规则限制
# 在mysql客户端执行下面语句
# 降低规则限制
set global validate_password.policy=0;
# 降低长度限制
set global validate_password.length=6;
# 设置新密码
alter user 'root'@'localhost' identified by '123456';
# 然后就可以正常创建数据库和表了


# 这时候只能本机连接使用mysql,可以查看user信息,都是被限制在了localhost
select * from mysql.user\G;
# 可以自己创建一个叫root的用户,%代表任何主机都可以访问,密码是123456
create user 'root'@'%' identified by '123456';
# 刚创建的用户是没有任何权限的,需要分配权限,可以把最高权限all分配给'root'@'%',前面的on *.*代表在任何数据库任何表下都是有all权限,这里可以指定具体的库和表
grant all on *.* to 'root'@'%'

# 如果你想要让其他机器连接数据库,那就得先把端口开放,默认端口3306是不开放的。
# 配置防火墙开放端口3306
sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重新加载防火墙配置
sudo firewall-cmd --reload

# 当你想远程连接数据库的时候,使用下列命令
mysql -h192.168.179.132 -uroot -p123456
# -h指的是host,输入服务器ip
# -u指的是user,输入用户名
# -p指的是password,输入密码
# 如果数据库不是运行在3306端口的,需要指定-P,这里是大写的P,指的是Port端口号

# 到这步已经可以正常使用数据库了,你可以用选择你喜欢的各种可视化工具

执行【yum install openssl-devel】发生的报错如下

log 复制代码
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"


 One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

解决方法如下(注意:阿里云的镜像在本文发布2025/03/20时依然能用,但是不确保以后这个仓库地址不会变动或者遗弃)

shell 复制代码
# 下载阿里云的配置文件
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
相关推荐
ajassi200011 分钟前
linux C 语言开发 (八) 进程基础
linux·运维·服务器
寻星探路22 分钟前
数据库造神计划第六天---增删改查(CRUD)(2)
java·大数据·数据库
..过云雨22 分钟前
05.【Linux系统编程】进程(冯诺依曼体系结构、进程概念、进程状态(注意僵尸和孤儿)、进程优先级、进程切换和调度)
linux·笔记·学习
matlab的学徒1 小时前
Web与Nginx网站服务(改)
linux·运维·前端·nginx·tomcat
Insist7531 小时前
prometheus安装部署与alertmanager邮箱告警
linux·运维·grafana·prometheus
盖世英雄酱581361 小时前
Read timed out问题 排查
java·数据库·后端
BAGAE2 小时前
MODBUS 通信协议详细介绍
linux·嵌入式硬件·物联网·硬件架构·iot·嵌入式实时数据库·rtdbs
灿烂阳光g2 小时前
SELinux 策略文件编写
android·linux
xqlily2 小时前
Linux操作系统之Ubuntu
linux·运维·ubuntu
阿部多瑞 ABU2 小时前
《基于国产Linux的机房终端安全重构方案》
linux·安全