1、安装前检查是否存在mysql
bash
yum list installed mysql*
①如或显示了列表,说明系统中有MySQL
**yum卸载 ** 根据列表上的名字([中括号为可选项])
bash
yum remove [填写列表显示出来的所有内容]
rm -rf /var/lib/mysql
rm /etc/my.cnf
②rpm查看安装
bash
rpm -qa | grep mysql
bash
rpm -e [列表筛选出来的依次卸载]
cd /var/lib/
rm -rf mysql/
③清除余项
bash
whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/local/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
#删除上面所有的文件夹
rm -rf /usr/bin/mysql
④删除配置
bash
rm -rf /etc/my.cnf
2、开始安装mysql
①首先,尝试一下直接使用 yum 安装 MySQL
bash
yum install mysql-community-server
如果成功,表示不需要配置MySQL rpm 源信息,直接就安装完成了
但是,如果出现以下错误:
表示我们没有添加安装包的源信息,需要安装 MySQL rpm 源信息
②安装 MySQL rpm 源信息
MySQL :: Download MySQL Yum Repository
bash
wget http://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
rpm -ivh mysql80-community-release-el7-11.noarch.rpm
③安装
再尝试使用 yum 安装MySQL
bash
yum install mysql-community-server
安装过程中,会提示让我们确认,一律输入**y
**按回车即可
安装完成后,yum会自动覆盖自带的mariaDB,所以不需要我们手动卸载它
④检查安装是否成功
检查一下刚才的安装是否成功
bash
rpm -qa | grep mysql
输出:
mysql-community-common-8.0.35-1.el7.x86_64
mysql-community-server-8.0.35-1.el7.x86_64
mysql-community-libs-8.0.35-1.el7.x86_64
mysql-community-client-8.0.35-1.el7.x86_64
mysql80-community-release-el7-11.noarch
mysql-community-icu-data-files-8.0.35-1.el7.x86_64
mysql-community-client-plugins-8.0.35-1.el7.x86_64
输出类似以上内容,表示安装完成
⑤检查mariaDB是否被覆盖
bash
rpm -qa | grep mariadb
输出空,表示 mariaDB 已经被成功覆盖。
3、修改配置文件my.cnf,复制下面这个即可,端口:3308
sql
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#绑定ipv4
bind-address=0.0.0.0
#默认3306端口,如需修改端口,设置端口
port=3308
#设置字符集为UTF8
character-set-server=utf8
#设置传输包限制,默认1M
max_allowed_packet=32M
#设置最大连接数
max_connections=500
#设置缓冲池大小:
innodb_buffer_pool_size=2048M
#设置密码验证默认加密方式
default-authentication-plugin=mysql_native_password
#设置不区分大小写
lower_case_table_names=1
#设置隔离级别
transaction-isolation=read-committed
#设置时区
default-time-zone='+8:00'
# 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
4、启动mysql
① 第一次启动后,可以查看mysql初始化密码
bash
grep 'temporary password' /var/log/mysqld.log
获取最后的密钥:
②登录mysql
sql
mysql -u root -p -P 3308
然后输出刚才得到的密钥:
③修改 root 密码
注意了,默认的密码策略,需要:大写英文 + 特殊字符 + 数字
sql
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root_123';
5、开放 root 账户远程登录
sql
#退出mysql
exit
# 登录
mysql -u root -p'密码'
# 如果你的数据库是 mysql 8 及以上
# 1、进入数据库
use mysql
# 2、修改user表
update user set host='%' where user='root';
# mysql 5.7 及之前,执行这行代码即可
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码' WITH GRANT OPTION;
# 重载授权表
FLUSH PRIVILEGES;
# 退出
exit
# 重启
systemctl restart mysqld