CentOS7 安装MySQL8
一、MySQL 安装包下载
- 进入MySQL官网,选择 DOWNLOADS
- 点击 MySQL Community (GPL) Downloads 链接进入下载页
- 选择 MySQL Community Server
- 选择版本(redhat 或者 linux-generic都行)
- 选择对应的64位版本 – Download
- 点击 No thanks, just start my download. 选择直接下载
二、MySQL安装
Ⅰ- mysql安装的前置准备工作
- 检查是否有安装mysql(如果有就进行卸载,我是新安装的CentOS7是没有)
rpm -qa | grep -i mysql
----找到mysql文件夹
find / -name mysql
whereis mysql
- 查看是否有mariaDB
rpm -qa|grep mariadb
//删除
rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
//删除my.cnf
rm -rf /etc/my.cnf
- 安装包上传到 CentOS 服务器上
- Mysql 有个推荐的默认安装路径 /usr/local ,先将 安装包移动到该目录下
[root@gone ~]# mv mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz /usr/local/
[root@gone ~]# cd /usr/local/
[root@gone local]# ls
bin etc games include lib lib64 libexec mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz sbin share src
- 解压该安装包
[root@gone local]# tar -xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
mysql-8.0.32-linux-glibc2.12-x86_64/bin/
mysql-8.0.32-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-8.0.32-linux-glibc2.12-x86_64/bin/myisamchk
mysql-8.0.32-linux-glibc2.12-x86_64/bin/myisamlog
mysql-8.0.32-linux-glibc2.12-x86_64/bin/myisampack
mysql-8.0.32-linux-glibc2.12-x86_64/bin/mysql
...
...
-
解压之后的文件
-
文件夹名称太长,直接修改为 mysql
[root@gone local]# mv mysql-8.0.32-linux-glibc2.12-x86_64 mysql
[root@gone local]# ls -lrt
total 596012
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
drwxr-xr-x. 5 root root 49 Jul 17 2021 share
-rw-r--r--. 1 root root 610315332 Jan 30 21:13 mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
drwxr-xr-x. 9 root root 129 Jan 30 21:29 mysql8
- 查看解压之后的大小为1.1个G
[root@gone local]# du -h --max-depth=1
- mysql 中各个目录的作用
bin 目录:一些可执行的命令,包括启动 mysql 服务,连接 mysql 的命令
docs目录: 一些相关的文档信息
include目录: mysql的头文件(mysql是由 C 和C++ 写的,所以这里面能看到 .h 的头文件)
lib目录: 用于放置一系列库文件
share目录: 用于存放字符集、语言等信息 ,错误信息和配置文件
support-files目录: 存放的是服务启动脚本
data目录: 存放mysql数据的目录,这个目录是需要自己创建的
[root@gone mysql]# mkdir data
mysql 默认保存数据的目录是/usr/local/mysql/data ,vim查看 mysql.server 脚本中可以发现默认保存数据的目录 datadir = /usr/local/mysql/data 就是我们创建的data目录
- 查看是否存在mysql 用户组 和创建用户组
[root@gone mysql]# cat /etc/group | grep mysql
[root@gone mysql]# cat /etc/passwd |grep mysql
- 创建mysql 用户组和用户
[root@gone mysql]# groupadd mysql
[root@gone mysql]# useradd -r -g mysql mysql
- 更改mysql 目录下所有文件夹所属的用户组和用户,以及权限
[root@gone mysql]# chown -R mysql:mysql /usr/local/mysql
[root@gone mysql]# chmod -R 777 /usr/local/mysql
Ⅱ- 常规安装
- 初始化mysql 数据(data),并生成一个临时密码
[root@gone mysql]# bin/mysqld --initialize --user=mysql --datadir /usr/local/mysql/data/
2023-01-30T14:26:12.069192Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 89205
2023-01-30T14:26:12.189064Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-30T14:26:12.863570Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-01-30T14:26:13.902710Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +wOx3EwCggMq
如果临时密码忘记了,需要重新初始化一次就可以
bin/mysqld --initialize --user=mysql --datadir /usr/local/mysql/data/
2023-01-30T14:29:39.629058Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 89471
2023-01-30T14:29:39.630835Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2023-01-30T14:29:39.630845Z 0 [ERROR] [MY-013236] [Server] The designated data directory /usr/local/mysql/data/ is unusable. You can remove all files that the server added to it.
2023-01-30T14:29:39.630892Z 0 [ERROR] [MY-010119] [Server] Aborting
2023-01-30T14:29:39.631032Z 0 [System] [MY-010910] [Server] /usr/local/mysql/bin/mysqld: Shutdown complete (mysqld 8.0.32) MySQL Community Server - GPL.
提示报错:The designated data directory /usr/local/mysql/data/ is unusable. You can remove all files that the server added to it. 是说data目录中有文件了,需要清空之后再初始化,即可成功
[root@gone data]# rm -rf *
[root@gone data]# ll
total 0
[root@gone data]# cd ..
[root@gone mysql]# bin/mysqld --initialize --user=mysql --datadir /usr/local/mysql/data/
2023-01-30T14:32:44.864266Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 89679
2023-01-30T14:32:44.874017Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-30T14:32:45.347413Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-01-30T14:32:46.371687Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: AwgZV)tPe38a
- 查看初始化后的 data 目录:
- 将 support-files 目录下的启动脚本 复制一份到 /etc/init.d/ 目录下 ,复制过去的脚本名称可以任取,比如像直接命名为 mysql
[root@gone init.d]# cp mysql.server /etc/init.d/mysql
- 启动mysql 服务
一般CentOS都自带mariaDB,此次mysql 安装是默认的常规安装配置,启动之前删 /etc/my.cnf 配置文件,然后启动 mysql 服务
[root@gone ~]# rm /etc/my.cnf
--启动 mysql
root@gone ~]# /etc/init.d/mysql start
Starting MySQL.Logging to '/usr/local/mysql/data/gone.err'.
. SUCCESS!
Ⅲ - 非常规安装
不安装在默认目录下
-
把在默认路径 /usr/local/ 下的mysql 文件夹移动到新创建的 /opt/database/ 路径
-
初始化 mysql 下的data目录(提示报错,是因为没有指定初始化的 datadir )
[root@gone mysql]# bin/mysqld --initialize --user=mysql
mysqld: Can't create directory '/usr/local/mysql/data/' (Errcode: 2 - No such file or directory)
2023-01-31T08:46:36.461736Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-01-31T08:46:36.461909Z 0 [ERROR] Can't find error-message file '/usr/local/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2023-01-31T08:46:36.462640Z 0 [ERROR] Aborting
解决办法:
☆ 初始化时指定 datadir和basedir ,可以看到初始化成功并生成了新的临时密码
[root@gone mysql]# bin/mysqld --initialize --user=mysql --datadir /opt/database/mysql/data --basedir=/opt/database/mysql
2023-01-31T08:48:08.174133Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-01-31T08:48:08.426141Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-01-31T08:48:08.480478Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-01-31T08:48:08.542534Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: fc421c0e-a143-11ed-8df1-000c2993dda5.
2023-01-31T08:48:08.544263Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-01-31T08:48:09.083424Z 0 [Warning] CA certificate ca.pem is self signed.
2023-01-31T08:48:09.162463Z 1 [Note] A temporary password is generated for root@localhost: j&RCjT5iCaWM
☆在 /etc/my.cnf 文件中指定初始化的 datadir和basedir
basedir=/opt/database/mysql
datadir=/opt/database/mysql/data
清空 /opt/database/mysql/data 目录再次执行初始化,可以看到初始化成功并生成了新的临时密码
[root@gone mysql]# /bin/mysqld --initialize --usr=mysql
-bash: /bin/mysqld: No such file or directory
[root@gone mysql]# bin/mysqld --initialize --user=mysql
2023-01-31T08:56:46.188168Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-01-31T08:56:46.368877Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-01-31T08:56:46.397377Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-01-31T08:56:46.454498Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 30f53460-a145-11ed-8e6f-000c2993dda5.
2023-01-31T08:56:46.455559Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-01-31T08:56:47.448128Z 0 [Warning] CA certificate ca.pem is self signed.
2023-01-31T08:56:47.820853Z 1 [Note] A temporary password is generated for root@localhost: y7%.EG4J5M7y
❗ 注意:上述两种方式中如果只是指定了datadir 但是不指定basedir 初始化可以成功但是会有一行报错的提示信息
- 启动mysql 服务
初始化data 目录是使用 'bin/mysqld --initialize --user=mysql --datadir /opt/database/mysql/data --basedir=/opt/database/mysql‘ 命令指定了basedir 和 datadir 的
,在启动mysql 服务之前需要在 /etc/my.cnf 中配置 basedir=/opt/database/mysql
否则启动会报错
[root@gone ~]# /etc/init.d/mysql start
/etc/init.d/mysql.server: line 239: my_print_defaults: command not found
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
初始化目录是通过 /etc/my.cnf 配置好了 datadir 和 basedir 则直接启动即可
[root@gone ~]# /etc/init.d/mysql start
Starting MySQL.Logging to '/opt/database/mysql/data/gone.err'.
SUCCESS!
如果启动之后,/etc/my.cnf 文件中的 basedir 被修改或者删除了,停止mysql 服务也会报错
[root@gone ~]# /etc/init.d/mysql stop
/etc/init.d/mysql.server: line 239: my_print_defaults: command not found
ERROR! MySQL server PID file could not be found!
- 连接MySQL
[root@gone ~]# mysql -u root -p
bash: mysql: command not found...
提示命令未找到,需要修改 .bash_profile ,指定现在的bin目录
PATH=$PATH:$HOME/bin:/opt/database/mysql/bin
修改完成后 执行 source .bash_profile 命令使文件立即生效
[root@gone ~]# source .bash_profile
再次执行登录mysql 命令,修改用户密码后再次登录
[root@gone ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32
Copyright (c) 2000, 2020, 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> alter user user() identified by 'mysql';
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
[root@gone ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
- 创建提供远程访问连接的用户并分配权限
mysql> create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'mysql';
Query OK, 0 rows affected (0.42 sec)
-- 赋权
mysql> grant all on *.* to 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
至此,Mysql安装完成