# Linux环境安装-MySQL安装

By [Yeanuo](https://paragraph.com/@yeanuo) · 2023-10-27

---

本文记录在CentOS中使用yum方式安装MySQL的详细步骤

**操作环境**
========

*   Linux版本：CentOS Linux release 7.9.2009
    
*   安装的MySQL版本：mysql-community-common.x86\_64 0:8.0.35-1.el7
    
*   安装路径：/usr/local/mysql/
    

yum方式安装
=======

*   从CentOS 7.0发布以来，yum源中开始使用Mariadb来代替MySQL的安装。即使你输入的是yum install -y mysql , 显示的也是Mariadb的安装内容。使用源代码进行编译安装又太麻烦。因此，如果想使用yum安装MySQL的话，就需要去下载官方指定的yum源。
    
*   yum下载网址为：[https://dev.mysql.com/downloads/repo/yum/，找到Red](https://dev.mysql.com/downloads/repo/yum/%EF%BC%8C%E6%89%BE%E5%88%B0Red) Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package，单击后面的Download，在新的页面中单击最下面的No thanks, just start my download.就可以下载到yum源了。
    

安装yum资源库
========

*   需要去官网下载最新的版本，否则可能因为版本更新依赖问题，导致安装失败
    
    `yum localinstall https://repo.mysql.com//mysql80-community-release-el7-11.noarch.rpm`
    

安装MySQL
=======

*   `yum install -y mysql-community-server`
    

![安装成功](https://storage.googleapis.com/papyrus_images/865587526e6bbdada5f03f2e064e3a90da610c5a9ac5a03f45a9404a15989b31.png)

安装成功

验证安装
====

*   `systemctl start mysqld.service`
    
*   `systemctl status mysqld.service`
    

![mysql status](https://storage.googleapis.com/papyrus_images/410e0b062b3359a831bd71e83bc2694d9f7bc291bfb630ed4b905f65fc5cc6fa.png)

mysql status

登录
==

*   由于MySQL从5.7开始不允许首次安装后使用空密码进行登录。为了加强安全性，系统会随机生成一个密码以供管理员首次登录使用，这个密码记录在/var/log/mysqld.log文件中，使用下面的命令可以查看此密码：
    
    `cat /var/log/mysqld.log|grep 'A temporary password'`
    
*   使用临时密码登录
    
    `mysql -p`
    
*   修改密码
    
    `mysql> alter user 'root'@'localhost' identified by '123456';`
    
*   刷新权限
    
    `mysql> flush privileges;`
    

验证
==

*   查看版本
    
    `mysql> select version();`
    

![mysql version](https://storage.googleapis.com/papyrus_images/8dce3ff55e2f3e593a6862d67d53251ff0f131cc7f8e872d3a4a18987bbd321e.png)

mysql version

开通3306端口
========

*   firewall-cmd --permanent --zone=public --add-port=3306/tcp

---

*Originally published on [Yeanuo](https://paragraph.com/@yeanuo/linux-mysql)*
