安装前

1.安装mysql前,需将系统自带的mariadb卸载。

[root@k8s01 ~]#  rpm ‐qa|grep mariadb
mariadb‐libs‐5.5.52‐1.el7.x86_643
[root@k8s01 ~]#  rpm ‐e ‐‐nodeps mariadb‐libs‐5.5.52‐1.el7.x86_644
[root@k8s01 ~]#  rpm ‐qa|grep mariadb

2. 下载mysql8.0.20

[root@k8s-01 src]# wget https://cdn.mysql.com/Downloads/MySQL-8.0/mysql-8.0.20-el7-x86_64.tar.gz

开始安装

解压mysql包

[root@k8s-01 src]# tar -zxvf mysql-8.0.20-el7-x86_64.tar.gz

创建安装目录

[root@k8s-01 src]# mkdir -p /usr/local/mysql
[root@k8s-01 src]# mv mysql-8.0.20-el7-x86_64/*  /usr/local/mysql/

创建mysql用户和组

[root@k8s-01 src]# groupadd mysql
[root@k8s-01 src]# useradd -r -g mysql -s /sbin/nologin mysql

创建数据目录并授权

[root@k8s-01 src]# cd /usr/local/mysql/
[root@k8s-01 mysql]# mkdir data logs
[root@k8s-01 mysql]# chown -R mysql:mysql /usr/local/mysql/

在etc下新建配置文件my.cnf,加入下面配置

[mysqld]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/logs/mysql.log
socket=/tmp/mysql.sock

event_scheduler=ON

slow_query_log = ON
slow_query_log_file = /usr/local/mysql/logs/mysql-slow.log
long_query_time = 1
innodb_buffer_pool_size = 6G

default-authentication-plugin=mysql_native_password
server-id=10
log-bin=/usr/local/mysql/logs/mysql-bin
max_connect_errors=1000

log-error=/usr/local/mysql/logs/mysql.log
pid-file=/usr/local/mysql/data/mysql.pid

安装数据库,生成初始密码并查看

[root@k8s-01 mysql]# cat logs/mysql | grep password
root@k8s-01 mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

在这里插入图片描述
配置mysql服务

[root@k8s-01 mysql]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 65536
LimitNPROC = 65536
[root@k8s-01 mysql]# systemctl  daemon-reload  #重新加载

软链接

[root@k8s-01 mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
[root@k8s-01 mysql]# ln -s /usr/local/mysql/bin/mysqldump /usr/bin/

启动服务并设置开机自启

[root@k8s-01 mysql]# systemctl stop mysqld
[root@k8s-01 mysql]# systemctl start mysqld
[root@k8s-01 mysql]# systemctl enable mysqld

在这里插入图片描述
查看运行状态

[root@k8s-01 mysql]# ps -ef | grep mysqld

在这里插入图片描述

[root@k8s-01 mysql]# systemctl status mysqld

在这里插入图片描述
修改root密码,并设置远程登录

[root@k8s-01 mysql]# mysql -uroot -p8RA__Li6TPt?

在这里插入图片描述

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)

mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐