一、安装之前检测系统是否有自带的MySQL
1#检查是否安装过MySQL
2rpm -qa | grep mysql
3
4#检查是否存在 mariadb 数据库(内置的MySQL数据库),有则强制删除
5rpm -qa | grep mariadb
6
7rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 #强制删除
8
9rpm -e --nodeps mariadb-5.5.68-1.el7.x86_64 #强制删除
二、准备安装
跳转local文件夹,安装wget
1cd /usr/local #安装包目录
2yum install wget
下载MySQL安装包,我这里以8.0.33版本为例
1wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar #下载8.0安装包
2
3mkdir mysql && tar -xvf mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar -C mysql && cd mysql #创建文件夹,把下载的安装包解压到该目录
三、安装MySQL
只是举例,我这里是把所有解压内容都装了,自己根据自己的情况安装。安装顺序如下
1rpm -ivh mysql-community-common-8.0.36-1.el7.x86_64.rpm
2rpm -ivh mysql-community-client-plugins-8.0.36-1.el7.x86_64.rpm
3yum -y install libaio
4rpm -ivh mysql-community-embedded-compat-8.0.36-1.el7.x86_64.rpm
5rpm -ivh mysql-community-libs-8.0.36-1.el7.x86_64.rpm
6rpm -ivh mysql-community-libs-compat-8.0.36-1.el7.x86_64.rpm
7yum install openssl-devel -y
8rpm -ivh mysql-community-devel-8.0.36-1.el7.x86_64.rpm
9rpm -ivh mysql-community-client-8.0.36-1.el7.x86_64.rpm
10rpm -ivh mysql-community-icu-data-files-8.0.36-1.el7.x86_64.rpm
11yum install net-tools -y
12rpm -ivh mysql-community-server-8.0.36-1.el7.x86_64.rpm
13yum install perl-JSON.noarch -y
14yum install perl-Test-*.noarch -y
15rpm -ivh mysql-community-test-8.0.36-1.el7.x86_64.rpm
16rpm -ivh mysql-community-debuginfo-8.0.36-1.el7.x86_64.rpm
四、初始化数据库
MySQL使用之前先初始化
1#初始化
2mysqld --initialize --console
3
4#目录授权,否则可能会启动失败
5chown -R mysql:mysql /var/lib/mysql/
6
7#启动mysql
8systemctl start mysqld.service
对应的重启、关闭mysql命令如下
1systemctl stop mysqld #停止
2systemctl restart mysqld #启动
查询mysql状态
显示Active: active (running) 表示成功启动
修改mysql的登录密码,修改密码前需要使用临时账户登录一下:
1cat /var/log/mysqld.log | grep password
使用临时密码登录mysql
然后输入自己的临时密码,回车登录,这里是不显示输出
登录进来后第一件事情,修改root的密码
1alter user 'root'@'localhost' identified with mysql_native_password by '你的密码';
mysql8对密码的强度有很高的要求,具体密码要求可使用以下命令查询
1SHOW VARIABLES LIKE ‘validate_password%’;
mysql已经安装完成。