- Windows 平台安装 MySQL
-
Linux 平台 Rocky 9 安装 MySQL
- binary package
- rpm package
- yum 源
- source package
Windows 11 和 Rocky 9 Linux 初体验 MySQL8.0.33 简易安装教程。
MySQL 8.0 目前最新稳定版本已经更新到了 MySQL 8.0.33,估计下一个稳定版本(大约在今年 7月份)推出。当然,这是我个人根据 MySQL 的发行注记做出的推测。
本篇总结作为对之前发出的 MySQL 系列博文 《MySQL8.0.28安装教程全程参考MySQL官方文档》补充说明。
注意:如果你想在 MySQL 官网获取 8.0.29 ,是找不到资源的。因为出现了重大问题,官网直接移除了下载资源。推荐跳过 MySQL 8.0.29,升级到更高的版本或者使用历史稳定版本。
Important
This release is no longer available for download. It was removed due to a critical issue that could cause data in InnoDB
tables having added columns to be interpreted incorrectly. Please upgrade to MySQL 8.0.30 instead.
Windows 平台安装 MySQL
Windows install MySQL 8.0.x (Archive zip) 简易安装教程
友情提示:从 MySQL 5.7 开始,Archive zip 默认不再提供默认的 my.ini 或者 my.cnf 配置文件,需要手动构建。
注意:x 代表使用 MySQL 8.0 某个具体版本,比如我演示:mysql-8.0.33-winx64.zip。
- 获取 mysql-8.0.33-winx64.zip:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.33-winx64.zip
- 解压免安装版 MySQL:unzip mysql-8.0.x-winx64.zip
- 切换到 MySQL 解压目录:cd mysql-8.0.x-winx64
- 新增 MySQL 配置文件: my.ini,详细配置可以参考官方文档,或者安装 msi 安装包,复制一份作为参考。
- 初始化 MySQL:
binmysqld --initialize-insecure
或者binmysqld --initialize-insecure --console
- 注册 MySQL 服务:
binmysqld --install MySQL80
(将 MySQL 服务注册到 service,可以使用 net 命令进行管理) - 启动 MySQL 服务:
net start MySQL80
或者sc start MySQL80
- 登录 MySQL 字符管理界面:mysql -uroot -p
Windows 环境新建 my.ini 做如下设置,指定基本安装目录(basedir)与数据存放目录(datadir)以及端口号(port):
[mysqld]
port=3307
basedir=D:\mysql-8.0.33-winx64
datadir=D:\mysql-8.0.33-winx64\data
为了便于演示,我设置 port 端口为 3307 ,登录时通过参数 -P 指定特定端口号。
复现在网上看到的一个问题,如果出现如下问题,如何解决?
PS D:mysql-8.0.33-winx64> binmysqld –initialize-insecure –console
[System] [MY-010116] [Server] D:mysql-8.0.33-winx64binmysqld.exe (mysqld 8.0.33) starting as process 12860
[Warning] [MY-010091] [Server]
Can't create test file D:mysql-8.0.33-winx64datamysqld_tmp_file_case_insensitive_test.lower-test
[Warning] [MY-010091] [Server]
Can't create test file D:mysql-8.0.33-winx64datamysqld_tmp_file_case_insensitive_test.lower-test
[ERROR] [MY-013376] [Server] Failed to set datadir to 'D:mysql-8.0.33-winx64data' (OS errno: 2 - No such file or directory)
[ERROR] [MY-010119] [Server] Aborting
[System] [MY-010910] [Server] D:mysql-8.0.33-winx64binmysqld.exe: Shutdown complete (mysqld 8.0.33) MySQL Community Server - GPL.
需要检查初始化命令参数是否有缺失:binmysqld.exe –initialize-insecure –console,才发现 Windows powershell 中从文档复制过来的 --
变成了 – 拼接在一起的单横杠。
注意:注意细节问题。如果出现初始化 data 目录失败,某某文件、目录无法创建等等问题。有可能是格式显示问题,复制粘贴参数时单 – 中横杠 和 --
双中横杠没有区分开来,如果放在代码块中正常显示。
如下是示例,初始化 MySQL 方式:
- 密码随机:
binmysqld --initialize
- 密码置空:
binmysqld --initialize-insecure
- 密码置空且将信息打印在字符命令行界面上:
binmysqld --initialize-insecure --console
binmysqld --initialize # 密码随机
binmysqld --initialize-insecure # 密码置空
binmysqld --initialize-insecure --console # 密码置空且将信息打印在字符命令行界面上
友情提示:打开 CMD 或者 PowerShell 时,请以管理员身份运行,如果没有,安装服务时则会提示权限拒绝,如下所示。
D:softwaremysql-8.0.33-winx64bin>mysqld –install MySQL80
Install/Remove of the Service Denied!
Windows 11 操作系统中安装 MySQL8.0.33 (noinstall Archive zip) 步骤
使用 Windows 自带的终端管理工具,打开 PowerShell:
PS D:work> cd D:workmysql-8.0.33-winx64
PS D:workmysql-8.0.33-winx64> .binmysqld --initialize-insecure --console
2023-05-21T07:04:03.420220Z 0 [System] [MY-013169] [Server] D:workmysql-8.0.33-winx64binmysqld.exe (mysqld 8.0.33) initializing of server in progress as process 13344
2023-05-21T07:04:03.465908Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-05-21T07:04:03.941998Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-05-21T07:04:05.242589Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
D:workmysql-8.0.33-winx64>binmysqld --install MySQL80
Service successfully installed.
D:workmysql-8.0.33-winx64>net start MySQL80
MySQL80 服务正在启动 .
MySQL80 服务已经启动成功。
PS D:workmysql-8.0.33-winx64> binmysql -uroot -p -P 3307
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
初步使用 MySQL
默认登录:
mysql -uroot -p
指定 ip 地址以及端口号:
mysql -uroot -p -h 192.168.245.133 -P 3307
参数含义:
- -u:指定用户名(user)
- -p:指定密码(password)
- -h:指定主机地址(host)
- -P:指定端口号(port)
PS D:workmysql-8.0.33-winx64> binmysql -uroot -p -P 3307
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> select 990 + 8 G
*************************** 1. row ***************************
990 + 8: 998
1 row in set (0.00 sec)
mysql> create database study;
Query OK, 1 row affected (0.01 sec)
mysql> use study;
Database changed
mysql> create table books(books_id int primary key,books_name varchar(64),isbn varchar(64),author varchar(33));
Query OK, 0 rows affected (0.02 sec)
mysql> select * from booksG
Empty set (0.01 sec)
mysql> insert into books(books_id,books_name,isbn,author) values(1001,'绝世武功秘籍','22-90-12345','不详');
Query OK, 1 row affected (0.00 sec)
mysql> select * from booksG
*************************** 1. row ***************************
books_id: 1001
books_name: 绝世武功秘籍
isbn: 22-90-12345
author: 不详
1 row in set (0.00 sec)
检查日志文件 :data directory host_name.err file.could not open the mysql.plugin table
711538Z 0 [System] [MY-013169] [Server] d:mysql-8.0.33-winx64binmysqld.exe (mysqld 8.0.33) initializing of server in progress as process 13908
746579Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
205845Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
399260Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.**
020963Z 0 [System] [MY-010116] [Server] D:mysql-8.0.33-winx64binmysqld (mysqld 8.0.33) starting as process 13756
038677Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
333181Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
552785Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
553344Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
583339Z 0 [System] [MY-011333] [Server] X Plugin ready for connections. Bind-address: '::' port: 33360
583379Z 0 [System] [MY-010931] [Server] D:mysql-8.0.33-winx64binmysqld: ready for connections. Version: '8.0.33' socket: '' port: 3307 MySQL Community Server - GPL.
085937Z 0 [System] [MY-013105] [Server] D:mysql-8.0.33-winx64binmysqld: Normal shutdown.
812152Z 0 [System] [MY-010910] [Server] D:mysql-8.0.33-winx64binmysqld: Shutdown complete (mysqld 8.0.33) MySQL Community Server - GPL.
发现问题:
could not open the mysql.plugin table
尝试解决问题:
- 检查配置文件:my.ini
- 分析问题,检查初始化和安装服务:
mysqld --initialize
或者mysqld --initialize-insecure --console
- 移除服务:
mysqld --remove MySQL80
- 移除数据目录:data
- 排查问题,再次初始化:
mysqld --initialize
或者mysqld --initialize-insecure --console
- 再次安装服务:
mysqld --install MySQL80
Linux 平台 Rocky 9 安装 MySQL
Rocky 9 Linux 平台 MySQL 安装方式:
- yum 源:使用 Linux 发行版自带的 yum 工具进行安装。
- rpm 包安装:最为简单,但不灵活,适合初学者使用。
- 二进制包(binary package):编译好的源码包,比 rpm 包更灵活。个人认为是安装多个服务最佳选择。
- 源码包(source package):最灵活,可根据需求编译安装功能,难易度最高。
- docker 形式安装:其实是在容器中安装。
如下,介绍二进制包、rpm 包、yum 源以及源码包形式 MySQL 简易安装教程。
binary package
01 binary package:使用 Linux 平台通用的二进制包安装
获取:mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz
https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz
解压:
tar -zxvf mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz
需要安装依赖包:
dnf install libaio
dnf -y install ncurses-compat-libs
如下是详细安装步骤:
2.2 Installing MySQL on Unix/Linux Using Generic Binaries
- 创建 mysql 组;
- 创建 mysql 用户并做软链接;
- 切换到 local 目录;
- 解压 tar 包 mysql 文件;
- 创建软链接,full-path-to-mysql-VERSION-OS 是你解压后 mysql 文件目录名称;
- 进入到 mysql 目录;
- 创建 mysql-files 目录;
- 赋予 mysql 用户 mysql-files 所属目录与所属组为 mysql;
- 赋予 mysql-files 权限750;
- 初始化并设置用户为 mysql,生成随机密码会打印在字符界面(使用
--initialize-insecure
则设置空密码); - 启动 ssl_rsa 验证;
- 启动 MySQL 服务;
- 复制 mysql.server 脚本服务到 Linux 环境 init.d 目录,便于管理。
$> groupadd mysql
$> useradd -r -g mysql -s /bin/false mysql
$> cd /usr/local
$> tar xvf /path/to/mysql-VERSION-OS.tar.xz
$> ln -s full-path-to-mysql-VERSION-OS mysql
$> cd mysql
$> mkdir mysql-files
$> chown mysql:mysql mysql-files
$> chmod 750 mysql-files
$> bin/mysqld --initialize-insecure --user=mysql
$> bin/mysql_ssl_rsa_setup
$> bin/mysqld_safe --user=mysql &
#Next command is optional
$> cp support-files/mysql.server /etc/init.d/mysql.server
如果在 RHEL 9 中默认没有安装 chkconfig,没有软连接 /etc/init.d/ 目录,请安装 chkconfig 管理工具
dnf -y install chkconfig-1.20-2.el9.x86_64
启动 MySQL 服务
systemctl start mysql.server
如果使用 systemctl 启动 MySQL 服务,出现/etc/rc.d/init.d/mysql.server:行239: /usr/local/mysql/bin/my_print_defaults
: 权限不够,Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
解决方案,配置 MySQL 环境变量。补充一点,此处只介绍设置全局生效环境变量,需要 root 用户权限,或者使用 sudo 提权。
[root@localhost mysql]# systemctl status mysql
● mysql.server.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysql.server; generated)
Active: active (exited) since Tue 2023-05-23 16:26:14 CST; 7min ago
Docs: man:systemd-sysv-generator(8)
CPU: 12ms
5月 23 16:26:14 localhost.localdomain systemd[1]: Starting LSB: start and stop MySQL...
5月 23 16:26:14 localhost.localdomain mysql.server[4244]: /etc/rc.d/init.d/mysql.server:行239: /usr/local/mysql/bin/my_print_defaults: 权限不够
5月 23 16:26:14 localhost.localdomain mysql.server[4237]: Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
5月 23 16:26:14 localhost.localdomain systemd[1]: Started LSB: start and stop MySQL.
则做如下设置,编辑:vim /etc/profile,需要 root 用户权限,或者使用 sudo vim /etc/profile:
# mysql home
MYSQL_HOME=/usr/local/mysql
PATH=$PATH:$MYSQL_HOME/bin
export MYSQL_HOME PATH
执行 source /etc/profile
或者 sudo source /etc/profile
命令,环境变量立即生效。
再次查看 MySQL 服务状态:
[root@localhost mysql]# systemctl status mysql
● mysql.server.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysql.server; generated)
Active: active (running) since Tue 2023-05-23 16:40:35 CST; 4s ago
Docs: man:systemd-sysv-generator(8)
Process: 5003 ExecStart=/etc/rc.d/init.d/mysql.server start (code=exited, status=0/SUCCESS)
Tasks: 39 (limit: 22833)
Memory: 370.1M
CPU: 765ms
CGroup: /system.slice/mysql.server.service
├─5014 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/localhost.localdo>
└─5153 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/pl>
5月 23 16:40:34 localhost.localdomain systemd[1]: Starting LSB: start and stop MySQL...
5月 23 16:40:35 localhost.localdomain mysql.server[5003]: Starting MySQL. SUCCESS!
5月 23 16:40:35 localhost.localdomain systemd[1]: Started LSB: start and stop MySQL.
设置开机自启
systemctl enable mysql.server
当然,你还可以通过这种方式启动与关闭 MySQL 服务:
support-files/mysql.server start
Starting MySQL.. SUCCESS!
support-files/mysql.server stop
Shutting down MySQL. SUCCESS!
使用 mysql.server
脚本,支持 start|stop|restart|reload|force-reload|status
多种命令操作:
- start:启动服务
- stop:停止服务
- restart:重启服务
- reload:重载服务
- force-reload:强制重载服务
- status:查看服务状态
[root@localhost mysql]# support-files/mysql.server
Usage: mysql.server {start|stop|restart|reload|force-reload|status} [ MySQL server options ]
登录 MySQL 字符命令终端(已经配置好环境变量)
mysql -uroot -p
登录 MySQL 字符命令终端(没有环境变量),切换到部署 MySQL 根目录,cd /usr/local/mysql
,然后执行如下命令:
bin/mysql -uroot -p
修改用户密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mypwd@123';
创建用户root,主机地址为localhost
CREATE USER 'root'@'localhost' IDENTIFIED BY 'Mypwd@123';
创建用户root,主机地址%,匹配所有
CREATE USER 'root'@'%' IDENTIFIED BY 'Mypwd@123';
授权root用户所有权限,即可使远程登录
GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
刷新权限
flush privileges;
更多权限细化设置请参考 MySQL8.0.x 官方文档第 6 章节 Security。
6.2 Access Control and Account Management
rpm package
02 rpm 包安装
Rocky 9 Linux 平台以 rpm 包形式安装 MySQL8.0.33。
获取:下载最新版本的 mysql-8.0.33-1.el9.x86_64.rpm-bundle.tar,将下面介绍的 rpm 包通过解压缩软件提取出来。
tar -xvf mysql-8.0.33-1.el9.x86_64.rpm-bundle.tar
友情提示:如今有一部分 Linux 发行版已经将 MariaDB 作为默认数据库软件。如果安装时勾选了数据库软件选项,你可能需要卸载 MariaDB 的依赖库:yum remove mariadb-libs 。
2.5 Installing MySQL on Linux RPM Packages
yum remove mariadb-libs
rpm -ivh mysql-community-client-plugins-8.0.33-1.el9.x86_64.rpm
rpm -ivh mysql-community-common-8.0.33-1.el9.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.33-1.el9.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.33-1.el9.x86_64.rpm
dnf -y install openssl-devel-1:3.0.7-6.el9_2.x86_64
rpm -ivh mysql-community-devel-8.0.33-1.el9.x86_64.rpm
rpm -ivh mysql-community-client-8.0.33-1.el9.x86_64.rpm
rpm -ivh mysql-community-server-8.0.33-1.el9.x86_64.rpm
注意:如果你安装 mysql-community-devel-8.0.33-1.el9.x86_64.rpm 提示依赖检测需要 pkgconfig ( openssl ),做如下操作
dnf -y install openssl-devel-1:3.0.7-6.el9_2.x86_64
启动服务
systemctl start mysqld
设置开机自启
systemctl enable mysqld
安装后,默认使用临时随机密码,保存在 /var/log/mysqld.log 文件中
[wzgy@localhost soft]$ sudo grep 'temporary password' /var/log/mysqld.log
2023-05-21T09:07:15.482313Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: WT(yyZgUB0xd
登录
mysql -uroot -p
修改 root 用户密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mypwd@833';
友情提示:密码组成规则,必须包含一位大写字母和特殊符号,且密码长度不低于 9 位。如果密码不符合设置规则,会提醒你当前设置的密码不安全:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
卸载就比较简单,可以使用 Rocky 9 Linux 自带的 dnf 管理工具:
dnf remove mysql
dnf remove mysql-community-client-plugins-8.0.33-1.el9.x86_64
dnf remove mysql-community-common-8.0.33-1.el9.x86_64
dnf remove mysql-community-icu-data-files-8.0.33-1.el9.x86_64
如果你觉得,使用 rpm 包这样安装太繁琐,尤其是缺乏相关依赖包很头疼。可以使用发行版自带的 yum 或者 dnf 工具快速安装。
yum 源
03 yum 源安装
Installing MySQL on Linux Using the MySQL Yum Repository
友情提示:Centos 8(RHEL 8) 或者更高版本可以使用 dnf 替代 yum
1. 下载:mysql80-community-release-el9-1.noarch.rpm
click https://repo.mysql.com//mysql80-community-release-el9-1.noarch.rpm
2. $> sudo yum install platform-and-version-specific-package-name.rpm
3. 基于 EL9 系统安装:$> sudo yum install mysql80-community-release-el9-{version-number}.noarch.rpm
4. 启动 MySQL Server:$> systemctl start mysqld
5. 检测 MySQL Server:$> systemctl status mysqld
6. 登录 mysql:$> mysql -uroot -p
7. 修改密码:$> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPwd833!';
source package
04 Source:源码包安装
2.9 Installing MySQL from Source
- 新增 mysql 管理组
- 新增 mysql 用户
- 解压 mysql 安装包
- 创建 bld 目录
- 切换到 bld 目录
- 编译安装:cmake .. make make install
- 进入源码包安装后的 mysql 目录
- 创建目录 mysql-files
- 赋予 mysql-files 目录所属组和所属用户为 mysql
- 赋予 mysql-files 目录 750 权限
- 执行安全命令:
bin/mysql_ssl_rsa_setup
- 启动 mysql 服务:
bin/mysqld_safe --user=mysql &
- 复制 mysql.server 脚本服务到 Linux 环境 init.d 目录,便于管理
简易安装命令参考如下:
#Preconfiguration setup
$> groupadd mysql
$> useradd -r -g mysql -s /bin/false mysql
#Beginning of source-build specific instructions
$> tar zxvf mysql-VERSION.tar.gz
$> cd mysql-VERSION
$> mkdir bld
$> cd bld
$> cmake ..
$> make
$> make install
#End of source-build specific instructions
#Postinstallation setup
$> cd /usr/local/mysql
$> mkdir mysql-files
$> chown mysql:mysql mysql-files
$> chmod 750 mysql-files
$> bin/mysqld --initialize --user=mysql
$> bin/mysql_ssl_rsa_setup
$> bin/mysqld_safe --user=mysql &
#Next command is optional
$> cp support-files/mysql.server /etc/init.d/mysql.server
启动服务
systemctl start mysql.server
设置开机自启
systemctl enable mysql.server
登录
mysql -uroot -p
以上总结,仅供参考哟!希望对你的学习或者工作帮助。
参考资料:
MySQL 官方文档第二章节:Chapter 2 Installing and Upgrading MySQL
MySQL8.0.x 官方文档第 6 章节 Security:6.2 Access Control and Account Management
—END—
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net