centos7下mysql8二进制安装
1.环境信息
-
操作系统: CentOS Linux release 7.4.1708 (Core)
-
mysql版本:mysql-8.0.18-el7-x86_64.tar.gz
2.安装
2.1 卸载默认安装mysql或mariadb
# 使用rpm -q xxx 不用带版本信息
[cai@centos7 mysql8]$ rpm -qa|grep mysql
[cai@centos7 mysql8]$ rpm -qa|grep mariadb
# 本机是最小安装,故没有
# 如果有安装移除命令: 一定是上述查询的结果有版本等信息
# rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
2.2下载与解压
[cai@centos7 mysql8]$ pwd
/usr/local/mysql8
[cai@centos7 mysql8]$ ll mysql-8.0.18-el7-x86_64.tar.gz
-rw-r--r--. 1 cai cai 774862067 3月 25 20:16 mysql-8.0.18-el7-x86_64.tar.gz
[cai@centos7 mysql8]$ tar -zxf mysql-8.0.18-el7-x86_64.tar.gz
2.3 创建用户与用户组
本次直接使用cai:cai
,不再创建
2.4 创建mysql的数据、日志等数据存储目录
[cai@centos7 project1]$ pwd
/app/mysql8/project1
[cai@centos7 project1]$ mkdir {data,log,var}
# 注意由于是使用cai:cai作为mysql8的用户,故上述三个目录cai用户要有权限
# 直接修改所属主:所属组
# 可以使用 chown -R cai:cai /app/mysql8/project1
2.5 添加环境变量
[cai@centos7 project1]$ sudo vim /etc/profile
# MYSQL8_HOME
MYSLQ8_HOME=/usr/local/mysql8/mysql-8.0.18-el7-x86_64
export PATH=$PATH:$MYSLQ8_HOME/bin
# 刷新生效
[cai@centos7 project1]$ source /etc/profile
2.6 配置mysql参数文件 my.cnf
注意默认可能有/etc/my.cnf
存在,备份 sudo mv /etc/my.cnf /etc/my.cnf_back
[cai@centos7 mysql-8.0.18-el7-x86_64]$ pwd
/usr/local/mysql8/mysql-8.0.18-el7-x86_64
[cai@centos7 mysql-8.0.18-el7-x86_64]$ vim my.cnf
# 添加如下内容
[mysql]
default-character-set=utf8mb4
socket=/app/mysql8/project1/var/mysql.sock
[mysqld]
port=3306
socket=/app/mysql8/project1/var/mysql.sock
basedir=/usr/local/mysql8/mysql-8.0.18-el7-x86_64
datadir=/app/mysql8/project1/data
character-set-server=utf8mb4
default-storage-engine=INNODB
innodb_buffer_pool_size=200M
max_allowed_packet=128M
explicit_defaults_for_timestamp=1
log-output=FILE
slow_query_log=ON
slow_query_log_file=/app/mysql8/project1/log/liandodb_query.err
long_query_time=5
log-error=/app/mysql8/project1/log/liandodb_error.err
default-authentication-plugin=mysql_native_password
2.7 初始化
mysqld --initialize --user=cai --basedir=/usr/local/mysql8/mysql-8.0.18-el7-x86_64/ --datadir=/app/mysql8/project1/data/
# 执行完后生成mysql默认root用户的密码
# 也可以自己查看,初始密码是gx41D69*GBD
[cai@centos7 log]$ vim /app/mysql8/project1/log/liandodb_error.err
[Note] [MY-010454] [Server] A temporary password is generated for root@localhost: gx41D69*GBD:
2.8 Cento7 制作systemd自启动服务
[cai@centos7 log]$ sudo vim /etc/systemd/system/mysql8d.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=cai
Group=cai
ExecStart=/usr/local/mysql8/mysql-8.0.18-el7-x86_64/bin/mysqld --defaults-file=/usr/local/mysql8/mysql-8.0.18-el7-x86_64/my.cnf
LimitNOFILE = 65536
LimitNPROC = 65536
# 刷新
[cai@centos7 log]$ sudo systemctl daemon-reload
#开机自启动
[cai@centos7 log]$ sudo systemctl enabled mysql8d
# 启动/查看/停止
[cai@centos7 log]$ sudo systemctl [start/status/stop] mysql8d
3. 使用mysql8
3.1 登录与使用
[cai@centos7 log]$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
# 添加如下参数登录
# 输入默认密码gx41D69*GBD
[cai@centos7 log]$ mysql -u root -p -h 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
# 修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'admin@123123';
Query OK, 0 rows affected (0.04 sec)
#刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.07 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from user where user='root';
+-----------+------+
| host | user |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.00 sec)
#创建测试用户并福泉
mysql> create USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.03 sec)
mysql> grant all privileges on *.* to 'test'@'%' with grant option;
Query OK, 0 rows affected (0.06 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
4.允许远程连接
# 关闭防火墙
# 禁止开机启动
[cai@centos7 bin]$sudo systemctl disabled firewalld
# 关闭防火墙
[cai@centos7 bin]$sudo systemctl stop firewalld