官网: https://www.mysql.com/
yum安装下载:https://dev.mysql.com/downloads/repo/yum/
二进制安装下载:https://downloads.mysql.com/archives/community/
安装包下载
- yum安装下载
打开网址(https://dev.mysql.com/downloads/repo/yum/)
- 二进制安装下载
打开网址(https://downloads.mysql.com/archives/community/)
安装
系统版本: CentOS Linux release 7.4.1708 (Core)
前置准备
- 卸载系统自带mariadb
rpm -qa|grep mariadb
rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
Yum安装
- 将下载好的rpm文件上传到服务器
- 安装yum源
rpm -ivh mysql84-community-release-el7-1.noarch.rpm
3.安装mysql服务
yum -y install mysql-community-server
4.启动mysql服务,并设置自启动
systemctl start mysqld
systemctl enable mysqld
5.查看mysql默认密码
cat /var/log/mysqld.log |grep password
6.修改默认root密码
mysql -uroot -p #执行后输入刚才日志中的密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Pwd@123.com'; #修改root密码
flush privileges;
二进制安装
1.将下载好的安装包上传到服务器
2.安装依赖
yum install -y libaio libaio-devel openssl
3.解压安装包,创建数据目录,创建服务用户
tar xvf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.28-linux-glibc2.12-x86_64 /usr/local/mysql
mkdir /data/mysql8.0.28/{data,binlog} /var/log/mysql
groupadd -r mysql&&useradd -M -r -s /sbin/nologin -g mysql mysql
/usr/local/ 可替换为任意部署位置
4.设置mysql命令环境变量
vim /etc/profile
# 文件末尾添加如下内容
PATH=$PATH:/usr/local/mysql/bin #/usr/local/mysql/bin需为实际mysql的bin目录绝对路径
# 刷新环境变量
source /etc/profile
5.编写mysql配置文件,目录授权
chown -R mysql:mysql /usr/local/mysql /data/mysql8.0.28 /var/log/mysql
vim /etc/my.cnf
[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/data/mysql8.0.28/data
pid-file=/usr/local/mysql/mysqld.pid
socket=/tmp/mysql.sock
max_connections=15000
log-error=/var/log/mysql/mysqld.err
character-set-server=utf8mb4
server-id=1
#read_only=1
# 二进制日志设置
log-bin=/data/mysql8.0.28/binlog/mysql-bin
# 二进制保存周期(s)
binlog_expire_logs_seconds=172800
# 中继日志设置
relay-log=relay-bin
relay_log_purge=0
relay-log-index=slave1-relay-bin.index
# 开启慢sql日志
slow_query_log=1
# 慢sql存储位置
slow_query_log_file=/var/log/mysql/mysql-slow_query.log
# 慢sql超过1s记录
long_query_time=1
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
#设置默认存储引擎为MYISAM(不设置为INNODB)
#default-storage-engine=MYISAM
#设置默认时区为上海
default-time_zone='+8:00'
#设置默认密码加密方式
default_authentication_plugin=mysql_native_password
#设置日志时间为系统时间(默认为UTC)
log_timestamps=system
[mysql]
default-character-set=utf8mb4
[client]
port=3306
socket=/tmp/mysql.sock
6.初始化数据库
mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql8.0.28/data/
7.设置systemctl,开机自启,启动服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add /etc/init.d/mysqld
chkconfig mysqld on
systemctl daemon-reload
systemctl start mysqld
systemctl enable mysqld
8.修改初始root密码
cat /var/log/mysql/mysqld.err |grep password
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Pwd@123.com';
flush privileges;