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
下面进行初始化时一定要加上–lower-case-table-names=1(不区分表名大小写),因为该参数是只读,后续改不了,只有在初始化的时候才能去修改,后续改不了,现在不改的话就后面就需要重装mysql,重装的过程需要从本文章第一节开始操作。
#执行初始化命令
/usr/sbin/mysqld --initialize --user=mysql --lower-case-table-names=1
# 初始化成功后,会生成mysql数据目录
ll /var/lib/mysql/
#mysql日志文件路径
ll /var/log/mysqld.log
修改配置文件,在配置文件后面追加以下内容
vim /etc/my.cnf
# 服务端默认字符集
character-set-server=utf8mb4
# 连接层默认字符集
collation-server=utf8mb4_unicode_ci
lower_case_table_names=1
[mysql]
# 数据库默认字符集
default-character-set=utf8mb4
[client]
# 客户端来源数据的默认字符集
default-character-set=utf8mb4
完整的配置文件内容如下:
# 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
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
lower-case-table-names=1
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# 服务端默认字符集
character-set-server=utf8mb4
# 连接层默认字符集
collation-server=utf8mb4_unicode_ci
#表名不区分大小写(这里也需要进行配置)
lower_case_table_names=1
[mysql]
# 数据库默认字符集
default-character-set=utf8mb4
#忽略表名大小写
lower_case_table_names = 1
[client]
# 客户端来源数据的默认字符集
default-character-set=utf8mb4
启动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啦!!!