1卸载mysql
#找出mysql依赖进行删除
rpm -qa | grep mysql
#找到后执行以下命令进行删除,直到删除干净
yum remove -y xxx
#找出mysql相关文件进行删除
find / -name mysql
#找到后执行以下命令进行删除,直到删除干净
rm -rf xxx
2下载mysql
官方下载地址:https://dev.mysql.com/downloads/mysql/
挑一个mysql8版本的进行下载,下载后上传到服务器。我这里Mysql文件是:
mysql-8.0.34-1.el7.x86_64.rpm-bundle.tar。 和我的版本不一样也问题不大,都是8版本就可以。
下载完毕后上传压缩包文件到服务器。
3安装mysql
关闭sellinux
#临时关闭
setenforce 0
#永久关闭 SELINUX
vim /etc/selinux/config
SELINUX=disabled
解压压缩包
tar -xvf mysql-8.0.34-1.el7.x86_64.rpm-bundle.tar
解压后可以看到有些文件
进行安装
安装方式一(安装依赖包过程得有先后顺序):
# 安装mysql-community-devel需要依赖openssl-devel,因此这里提前进行安装
yum install openssl-devel
rpm -ivh mysql-community-common-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.34-1.el7.x86_64.rpm
# 如果下面“mysql-community-libs”安装报错,可以执行 yum remove -y mariadb-libs
rpm -ivh mysql-community-libs-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-8.0.34-1.el7.x86_64.rpm
安装方式二(推荐。无需考虑依赖包先后顺序问题,并且会智能处理冲突,如 移除冲突的mariadb-libs组件等):
yum localinstall mysql*.rpm
安装详细过程:
# 由于我不需要debug和test这两个mysql组件,故而先给他移走,避免后续被安装上。
[root@localhost ~]# mkdir not_need_rmp_pkg
[root@localhost ~]# mv mysql-community-debuginfo-8.0.34-1.el7.x86_64.rpm not_need_rmp_pkg/
[root@localhost ~]# mv mysql-community-test-8.0.34-1.el7.x86_64.rpm not_need_rmp_pkg/
[root@localhost ~]# mv mysql-community-debuginfo-8.0.34-1.el7.x86_64.rpm mysql/
#==================================正式安装================================
[root@localhost ~]# yum localinstall mysql*.rpm
已加载插件:fastestmirror, langpacks
正在检查 mysql-community-client-8.0.34-1.el7.x86_64.rpm: mysql-community-client-8.0.34-1.el7.x86_64
mysql-community-client-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-client-plugins-8.0.34-1.el7.x86_64.rpm: mysql-community-client-plugins-8.0.34-1.el7.x86_64
mysql-community-client-plugins-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-common-8.0.34-1.el7.x86_64.rpm: mysql-community-common-8.0.34-1.el7.x86_64
mysql-community-common-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-devel-8.0.34-1.el7.x86_64.rpm: mysql-community-devel-8.0.34-1.el7.x86_64
mysql-community-devel-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-embedded-compat-8.0.34-1.el7.x86_64.rpm: mysql-community-embedded-compat-8.0.34-1.el7.x86_64
mysql-community-embedded-compat-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-icu-data-files-8.0.34-1.el7.x86_64.rpm: mysql-community-icu-data-files-8.0.34-1.el7.x86_64
mysql-community-icu-data-files-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-libs-8.0.34-1.el7.x86_64.rpm: mysql-community-libs-8.0.34-1.el7.x86_64
mysql-community-libs-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-libs-compat-8.0.34-1.el7.x86_64.rpm: mysql-community-libs-compat-8.0.34-1.el7.x86_64
mysql-community-libs-compat-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-server-8.0.34-1.el7.x86_64.rpm: mysql-community-server-8.0.34-1.el7.x86_64
mysql-community-server-8.0.34-1.el7.x86_64.rpm 将被安装
正在检查 mysql-community-server-debug-8.0.34-1.el7.x86_64.rpm: mysql-community-server-debug-8.0.34-1.el7.x86_64
mysql-community-server-debug-8.0.34-1.el7.x86_64.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mysql-community-client.x86_64.0.8.0.34-1.el7 将被 安装
---> 软件包 mysql-community-client-plugins.x86_64.0.8.0.34-1.el7 将被 安装
---> 软件包 mysql-community-common.x86_64.0.8.0.34-1.el7 将被 安装
---> 软件包 mysql-community-devel.x86_64.0.8.0.34-1.el7 将被 安装
---> 软件包 mysql-community-embedded-compat.x86_64.0.8.0.34-1.el7 将被 安装
---> 软件包 mysql-community-icu-data-files.x86_64.0.8.0.34-1.el7 将被 安装
---> 软件包 mysql-community-libs.x86_64.0.8.0.34-1.el7 将被 舍弃
---> 软件包 mysql-community-libs-compat.x86_64.0.8.0.34-1.el7 将被 舍弃
---> 软件包 mysql-community-server.x86_64.0.8.0.34-1.el7 将被 安装
---> 软件包 mysql-community-server-debug.x86_64.0.8.0.34-1.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
=========================================================================================================================================================================================================================================
Package 架构 版本 源 大小
=========================================================================================================================================================================================================================================
正在安装:
mysql-community-client x86_64 8.0.34-1.el7 /mysql-community-client-8.0.34-1.el7.x86_64 79 M
mysql-community-client-plugins x86_64 8.0.34-1.el7 /mysql-community-client-plugins-8.0.34-1.el7.x86_64 20 M
mysql-community-common x86_64 8.0.34-1.el7 /mysql-community-common-8.0.34-1.el7.x86_64 10 M
mysql-community-devel x86_64 8.0.34-1.el7 /mysql-community-devel-8.0.34-1.el7.x86_64 9.9 M
mysql-community-embedded-compat x86_64 8.0.34-1.el7 /mysql-community-embedded-compat-8.0.34-1.el7.x86_64 17 M
mysql-community-icu-data-files x86_64 8.0.34-1.el7 /mysql-community-icu-data-files-8.0.34-1.el7.x86_64 3.9 M
mysql-community-libs x86_64 8.0.34-1.el7 /mysql-community-libs-8.0.34-1.el7.x86_64 7.5 M
替换 mariadb-libs.x86_64 1:5.5.68-1.el7
mysql-community-libs-compat x86_64 8.0.34-1.el7 /mysql-community-libs-compat-8.0.34-1.el7.x86_64 3.6 M
替换 mariadb-libs.x86_64 1:5.5.68-1.el7
mysql-community-server x86_64 8.0.34-1.el7 /mysql-community-server-8.0.34-1.el7.x86_64 295 M
mysql-community-server-debug x86_64 8.0.34-1.el7 /mysql-community-server-debug-8.0.34-1.el7.x86_64 120 M
事务概要
=========================================================================================================================================================================================================================================
安装 10 软件包
总计:566 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : mysql-community-common-8.0.34-1.el7.x86_64 1/11
正在安装 : mysql-community-client-plugins-8.0.34-1.el7.x86_64 2/11
正在安装 : mysql-community-libs-8.0.34-1.el7.x86_64 3/11
正在安装 : mysql-community-client-8.0.34-1.el7.x86_64 4/11
正在安装 : mysql-community-icu-data-files-8.0.34-1.el7.x86_64 5/11
正在安装 : mysql-community-server-8.0.34-1.el7.x86_64 6/11
正在安装 : mysql-community-server-debug-8.0.34-1.el7.x86_64 7/11
正在安装 : mysql-community-devel-8.0.34-1.el7.x86_64 8/11
正在安装 : mysql-community-libs-compat-8.0.34-1.el7.x86_64 9/11
正在安装 : mysql-community-embedded-compat-8.0.34-1.el7.x86_64 10/11
正在删除 : 1:mariadb-libs-5.5.68-1.el7.x86_64 11/11
验证中 : mysql-community-server-8.0.34-1.el7.x86_64 1/11
验证中 : mysql-community-devel-8.0.34-1.el7.x86_64 2/11
验证中 : mysql-community-client-plugins-8.0.34-1.el7.x86_64 3/11
验证中 : mysql-community-server-debug-8.0.34-1.el7.x86_64 4/11
验证中 : mysql-community-icu-data-files-8.0.34-1.el7.x86_64 5/11
验证中 : mysql-community-embedded-compat-8.0.34-1.el7.x86_64 6/11
验证中 : mysql-community-common-8.0.34-1.el7.x86_64 7/11
验证中 : mysql-community-libs-8.0.34-1.el7.x86_64 8/11
验证中 : mysql-community-libs-compat-8.0.34-1.el7.x86_64 9/11
验证中 : mysql-community-client-8.0.34-1.el7.x86_64 10/11
验证中 : 1:mariadb-libs-5.5.68-1.el7.x86_64 11/11
已安装:
mysql-community-client.x86_64 0:8.0.34-1.el7 mysql-community-client-plugins.x86_64 0:8.0.34-1.el7 mysql-community-common.x86_64 0:8.0.34-1.el7 mysql-community-devel.x86_64 0:8.0.34-1.el7
mysql-community-embedded-compat.x86_64 0:8.0.34-1.el7 mysql-community-icu-data-files.x86_64 0:8.0.34-1.el7 mysql-community-libs.x86_64 0:8.0.34-1.el7 mysql-community-libs-compat.x86_64 0:8.0.34-1.el7
mysql-community-server.x86_64 0:8.0.34-1.el7 mysql-community-server-debug.x86_64 0:8.0.34-1.el7
替代:
mariadb-libs.x86_64 1:5.5.68-1.el7
完毕!
下面进行初始化时一定要加上–lower-case-table-names=1(不区分表名大小写),因为该参数是只读,后续改不了,只有在初始化的时候才能去修改,后续改不了,现在不改的话就后面就需要重装mysql,重装的过程需要从本文章第一节开始操作。
# 创建数据目录
mkdir -p /data/mysql
#执行初始化命令(这里指定了数据目录)
/usr/sbin/mysqld --initialize --user=mysql --lower-case-table-names=1 --datadir=/data/mysql
# 初始化成功后,会生成mysql数据目录
ll /data/mysql
#mysql日志文件路径
ll /var/log/mysqld.log
修改配置文件 /etc/my.cnf,完整的配置文件内容如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# 指定mysql的加密插件
default-authentication-plugin=mysql_native_password
# 指定mysql数据目录位置
datadir=/data/mysql
socket=/data/mysql/mysql.sock
#指定mysql日志文件位置
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# 服务端默认字符集
character-set-server=utf8mb4
# 设置 MySQL 服务器的默认排序规则(collation)为 utf8mb4_general_ci。
# 排序规则决定了如何比较和排序字符串。_general_ci 表示不区分大小写(case
collation-server=utf8mb4_unicode_ci
#表名不区分大小写(这里也需要进行配置)
lower_case_table_names=1
[mysql]
# 设置 mysql命令行客户端 的字符集
default-character-set=utf8mb4
[client]
# 客户端连接 MySQL 服务器时使用的字符集
default-character-set=utf8mb4
# 客户端连接时需要使用mysql.sock,故而也要指定
socket=/data/mysql/mysql.sock
启动mysql服务
systemctl start mysqld
systemctl enable mysqld
查看mysql生成的临时密码
cat /var/log/mysqld.log
登录mysql
#登录mysql服务
mysql -uroot -p
#此时会提示用户输入密码,输入上面生成的那个临时密码
#登录成功后进行以下操作
#设置密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxx';
#切到mysql库
use mysql;
#查看root 主机信息
select user,host from user;
#将root的主机设置成%,这样外部通过工具、程序等才能访问mysql服务
update user set host='%' where user='root';
#刷新权限
flush privileges;
至此大功告成,可以用客户端工具去连Mysql啦!!!