本文介绍Centos7以rpm方式安装MySQL8.0,其他相关参考
《Centos7通过yum下载MariaDB》:https://blog.csdn.net/sunny05296/article/details/78776765
《Centos7上卸载&安装MariaDB数据库》:https://blog.csdn.net/sunny05296/article/details/56015884
1.检查系统是否自带MariaDB或其他版本的MySQL
rpm -qa |grep MariaDB
rpm -qa |grep mariadb #有些mariadb版本是小写的
rpm -qa |grep mysql
2)如果有MariaDB先卸载MariaDB
选择对应的卸载方法
yum -y remove MariaDB*
或
yum -y remove mariadb*
或
rpm -e packagename --nodeps
3)删除数据库文件
rm -rf /var/lib/mysql/*
默认安装的数据库路径是在/var/lib/mysql/目录下
2.如果net-tools库没有安装,yum安装一下
yum -y install net-tools*
3.下载完整rpm集合包
https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-1.el7.x86_64.rpm-bundle.tar
4.解压
tar -xvf https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-1.el7.x86_64.rpm-bundle.tar
解压后的文件列表如下:
mysql-8.0.11-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.11-1.el7.x86_64.rpm
mysql-community-common-8.0.11-1.el7.x86_64.rpm
mysql-community-devel-8.0.11-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.11-1.el7.x86_64.rpm
mysql-community-libs-8.0.11-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.11-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-8.0.11-1.el7.x86_64.rpm
mysql-community-server-8.0.11-1.el7.x86_64.rpm
mysql-community-server-minimal-8.0.11-1.el7.x86_64.rpm
mysql-community-test-8.0.11-1.el7.x86_64.rpm
5.添加用户和组
groupadd mysql && useradd -g mysql mysql
6.安装
简单安装mysql-server服务,只需要安装下面4个软件包即可
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 #依赖于common
rpm -ivh mysql-community-client-8.0.11-1.el7.x86_64.rpm #依赖于libs
rpm -ivh mysql-community-server-8.0.11-1.el7.x86_64.rpm #依赖于client & common
如果安装过程中遇到libaio依赖库报错,则先安装libaio:
wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libaio-0.3.107-10.el6.x86_64.rpm
rpm -Uvh libaio-0.3.107-10.el6.x86_64.rpm
7.初始化MySQL数据库
mysqld --initialize #该命令会在/var/log/mysqld.log生成随机密码
cat /var/log/mysqld.log |grep pass #查看初始密码
8.配置数据库
修改mysql数据库目录的所属的用户和组属性
chown mysql:mysql /var/lib/mysql -R
默认MySQL的配置文件路径为:/etc/my.cnf,如果有需要可以修改配置文件
9.启动MySQL数据库
systemctl start mysqld #启动mysqld.service
systemctl status mysqld #查看状态
10.更改root用户的初始密码
mysql_secure_installation #根据提示修改MySQL的root账号密码
新版MySQL必须先修改密码,否则登录后是不能执行任何命令
11.本地访问MySQL
mysql -uroot -pxxxx
12.添加用户和远程授权
CREATE USER 'frank'@'%' IDENTIFIED BY '1q2w3e';
GRANT ALL ON *.* TO 'frank'@'%';
如果数据库设置了密码复杂策略检查,则密码要符合复杂度要求
==================附==================
1.navicat 连接MySQL8.0时报错:Client does not support authentication protocol requested by server.
解决办法:
mysql> use mysql;
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | frank | caching_sha2_password | $A$005$A*@p@|pnTOYyIG2/Z0G3LtWo4ETnuvgsbW68nTIgYz0ev32RkHb2UEA |
| % | test | caching_sha2_password | $A$005$X0?'/;&iM3?27EAVMHwb/ll9aqELvaqyUKrdM7XofcoSMJosH0jRA0y5 |
......
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
ALTER USER 'frank'@'%' IDENTIFIED WITH mysql_native_password by '1q2w3e';
ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password by '1q2w3e';
flush privileges; --或重启数据库
把用户的plugin设置由 caching_sha2_password 改为 mysql_native_password,然后重启数据库,就可以了。
2.配置文件my.cnf
vim /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 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
#
# 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
#
general_log=0
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#character
#character_set_server=utf8mb4 #default
#collation_server=utf8mb4_0900_ai_ci
#
#innodb_buffer_pool_size=134217728 #default, unit:Bytes
#innodb_buffer_pool_size=1342177280
innodb_buffer_pool_size=2G
#innodb_log_file_size=50331648 #default, unit:Bytes
#innodb_log_file_size=1024000000
#innodb_log_buffer_size=16777216 #default, unit:Bytes
#innodb_log_buffer_size=256000000
innodb_log_buffer_size=512M
#innodb_flush_log_at_trx_commit=1 #default
#innodb_flush_log_at_trx_commit=2
innodb_flush_log_at_trx_commit=0
#for insert performance improvement, default is 8M if not set, unit:Bytes
#bulk_insert_buffer_size=8388608 #default:8*1024*1024(8MB)
bulk_insert_buffer_size=100M
#innodb_flush_method=Default #default
#innodb_flush_method=O_DIRECT
#thread_cache_size=0 #default
thread_cache_size=32
#innodb_autoextend_increment=8 #default, unit:MB
innodb_autoextend_increment=128