安装前
1.安装mysql前,需将系统自带的mariadb卸载。
[root@k8s01 ~]# rpm ‐qa|grep mariadb
mariadb‐libs.x86_643
[root@k8s01 ~]# rpm ‐e ‐‐nodeps mariadb‐libs.x86_643
[root@k8s01 ~]# rpm ‐qa|grep mariadb
2. 下载mysql8.0.20
[root@k8s-01 ~]# 下载地址 https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.20-el7-x86_64.tar.gz
开始安装
解压mysql包[root@k8s-01 ~]# tar -zxvf mysql-8.0.20-el7-x86_64.tar.gz
创建安装目录[root@k8s-01 ~]# mkdir -p /usr/local/mysql
[root@k8s-01 ~]# mv mysql-8.0.20-el7-x86_64/* /usr/local/mysql/
创建mysql用户和组[root@k8s-01 ~]# groupadd mysql
[root@k8s-01 ~]# useradd -r -g mysql -s /sbin/nologin mysql
创建数据目录并授权[root@k8s-01 ~]# 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=3306basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/logs/mysql.log
socket=/tmp/mysql.sockevent_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 = 6Gdefault-authentication-plugin=mysql_native_password
server-id=10
log-bin=/usr/local/mysql/logs/mysql-bin
max_connect_errors=1000log-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
1
[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 -p5qfvsdrfkb?O
#修改密码 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)