MySQL安装
MySQL安装
Linux
CentOS or RHEL or Fedora ( Old version )
1、删除MariaDB
官方的 CentOS7 镜像会自带一套 mariadb ,如果是阿里的 CentOS ECS 服务器则不存在这个问题。
# 查看是否有mariaDB
sudo rpm -qa | grep mariadb
# 删除
sudo rpm -e mariadb-libs-5.5.68-1.el7.x86_64
# 如果上面那条语句不行,就强制删除
sudo rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
# 再检查一遍
sudo rpm -qa | grep mariadb
2、安装MySQL
# 查看是否安装MySQL
sudo rpm -qa | grep mysql
# 选择要下载的MySQL版本,进入官网进行下载rpm包
2.1、MySQL官网查找自己需要下载的MySQL版本
最新版MySQL:https://dev.mysql.com/downloads/mysql/
旧版MySQL:https://downloads.mysql.com/archives/community/
# 查看版本
cat /etc/redhat-release
# 查看Linux系统的内核版本
uname -r
# 选择如下包

2.2、安装MySQL
MySQL提供了多种软件安装方式
1、rpm方式,将软件包下载到指定目录下,在该目录下执行“rpm -ivh 包名”命令。支持离线
2、deb方式,下载软件包,执行“dpkg -i 包名”命令。支持离线
3、yum方式,确定正常联网,执行“yum install 软件包名”命令等等。不支持离线
# 将下好的rpm包导入Linux,然后复制到对应的目录,这里我选择opt目录,一般情况下,自己下载的软件都放在这里
cd /opt
# 创建存储目录
mkdir mysql5
# 移动,所有5.0的MySQL所需文件
mv /home/marshio/Downloads/mysql-community-*-5* mysql5
# 依次安装,需按照顺序
sudo rpm -ivh mysql-community-common-5.7.38-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-5.7.38-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-client-5.7.38-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-server-5.7.38-1.el7.x86_64.rpm
-i:表示安装,
-v:表示显示安装过程,
-h:表示显示进度

2.3、初始化MySQL
修改配置
# 创建data目录和log目录
mkdir {data,log}
# 修改配置文件
sudo vim /etc/my.cnf
# 源文件
[mysqld]
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
# 修改之后
[mysqld]
datadir=/opt/mysql5/data
socket=/opt/mysql5/data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/opt/mysql5/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# 初始化
mysqld --initialize
# 给对应的文件
# 启动服务
sudo systemctl start mysqld
# 或者
service mysqld start
# 如果启动失败,则需要查看日志
# 首先查看日志位置,找到 log-error 的文件位置,并查看,一般情况下MySQL的日志文件位于/var/log/mysqld.log,找到[ERROR]
cat /etc/my.cnf
cat /var/log/mysqld.log
# 如果提示类似不能写入,没有权限等问题,则需要赋予相应权限
chown mysql:mysql -R filename
开启端口
firewall-cmd
#
sudo firewall-cmd --list-ports
sudo firewall-cmd --zone=public --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
修改密码
# 查看默认密码,默认密码是在MySQL启动时自动给root用户创建的
cat /etc/my.cnf
cat /var/log/mysqld.log | grep 'password'
mysql -uroot -p
# 修改默认密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Abc123...'
# 修改root访问IP路径为所有
UPDATE user SET host='%' WHERE user='root';
# 刷新访问权限,或者重启MySQL
FLUSH PRIVILEGES
1ryh8QL+A*kg
CentOS or RHEL or Fedora ( with yum or dnf )
参考官网:linux installation yum repo
1、添加 YUM Repository
在 MySQL Yum Repository 页面选择并下载合适的版本包,包文件格式为:mysql84-community-release-{platform}-{version-number}.noarch.rpm
# 下载 rpm
wget https://dev.mysql.com/get/mysql84-community-release-el9-2.noarch.rpm
# 添加 rpm 包(如果你是 dnf 包管理工具,那么就用 dnf 替换掉 yum,下同)
yum localinstall mysql84-community-release-el9-2.noarch.rpm
# 查看并确认
yum repolist enabled | grep mysql.*-community
# 示例输出
mysql-8.4-lts-community MySQL 8.4 LTS Community Server
mysql-connectors-community MySQL Connectors Community
mysql-tools-8.4-lts-community MySQL Tools 8.4 LTS Community
mysql84: Indicates the MySQL version that is enabled by default. In this case, MySQL 8.4 is enabled by default, and both MySQL 8.0 and the MySQL Innovation series are available but disabled by default.
{platform}: The platform code, such as el7, el8, el9, fc41, or fc42. The 'el' represents Enterprise Linux, 'fc' for Fedora, and it ends with the platform's base version number. Confirm it with command
uname -a
.{version-number}: Version of the MySQL repository configuration RPM as they do receive occasional updates.
2、下载 MySQL
# 如果你是 el8
yum module disable mysql
# 然后就下载就行了
yum install mysql-community-server
3、启用 MySQL 服务
# 启用 mysql 服务
systemctl start mysqld
# 查看 mysql 服务状态,Active 为 active (running) 即表示成功
systemctl status mysql
4、查看 MySQL 生成的密码
# 默认 mysqld 的启动日志会放在这里
tail /var/log/mysqld.log
# 获取密码 sCZHcqk.W2He
mysql -uroot -psCZHcqk.W2He