文章目录
一、在线安装1、替换网易yum源2、清理缓存3、下载rpm文件4、安装MySQL数据库二、本地安装1、上传MySQL安装包2、安装依赖的程序包3、卸载mariadb程序包4、安装MySQL程序包5、修改MySQL目录权限6、初始化MySQL三、启动MySQL1.1、在线安装方式启动MySQL1.2、本地安装方式启动MySQL2、查看初始密码3、登陆数据库之后,修改默认密码4、允许远程使用root帐户5、允许远程访问MySQL数据库( vi /etc/my.cnf)6、开启防火墙3360端口
一、在线安装
1、替换网易yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo mirrors.163.com/.help/CentOS7-Base-163.repo

2、清理缓存
yum clean all
yum makecache

3、下载rpm文件
yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm

4、安装MySQL数据库
yum install mysql-community-server -y

二、本地安装
1、上传MySQL安装包
下载链接:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-1.el7.x86_64.rpm-bundle.tar
上传 MySQL 安装文件上传到 Linux 主机的 /root/mysql 目录,然后执行解压缩:
tar xvf mysql-8.0.11-1.el7.x86_64.rpm-bundle.tar

2、安装依赖的程序包
yum install perl -y
yum install net-tools -y

3、卸载mariadb程序包
rpm -qa|grep mariadb
rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 --nodeps
4、安装MySQL程序包
rpm -ivh mysql-community-common-8.0.11-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.11-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.11-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.11-1.el7.x86_64.rpm
补充:上边这些安装包都在 解压后的 MySQL 安装包中。

5、修改MySQL目录权限
chmod -R 777 /var/lib/mysql/
6、初始化MySQL
mysqld --initialize
/usr/sbin/mysqld --initialize --user=mysql --lower-case-table-names=1
chmod -R 777 /var/lib/mysql/*
三、启动MySQL
1.1、在线安装方式启动MySQL
service mysql start
1.2、本地安装方式启动MySQL
service mysqld start
在线与本地安装启动的唯一区别就是 mysql 是否包含字母 d
2、查看初始密码
grep 'temporary password' /var/log/mysqld.log
由于我们并不知道安装后的密码,所以执行如上指令后,会生成 root 的临时密码。

3、登陆数据库之后,修改默认密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
或者
alter user 'root'@'localhost' identified by '123456';

4、允许远程使用root帐户
use mysql;
UPDATE user SET host = '%' WHERE user ='root';
FLUSH PRIVILEGES;

5、允许远程访问MySQL数据库( vi /etc/my.cnf)
character_set_server = utf8
bind-address = 0.0.0.0

修改完配置后,重启一下MySQL,service mysqld restart
6、开启防火墙3360端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
