关于版本的选择,仅供参考 :
从版本稳定性和维护生命周期两个角度考虑。
从长远考虑:建议选择稳定长期支持版(Stable long term support versions),稳定且有更长的维护生命周期。
想尝试新特性,但又想有一定的稳定性:建议选择稳定短期支持版(Stable short term support versions),生命周期较短。
想尝试新特性:可以选择预览版(Preview release)和候选版(RC versions),不稳定。
企业或者个人生产环境,建议使用稳定版 (GA)。不推荐使用预览版(Preview release)和候选版(RC versions)。
mariadb 下载地址:https://mariadb.org/download
mariadb 官方文档:https://mariadb.org/documentation/
在线文档:https://mariadb.com/kb/en/documentation/
mariadb 官方文档 PDF 下载地址:MariaDBServerKnowledgeBase.pdf
mariadb 资源整合:https://pan.baidu.com/s/1YXhEunf4m8GREn2GwarYlw?pwd=qe7f
MariaDB 资料整理,包含 Windows 平台与 Linux 平台 mariadb 安装包,mariadb 离线 PDF 文档,JD-GUI 用于反编译查看 MariaDB Connector/J jar 包代码,以及个人整理的 mariadb 私人笔记:mariadb 笔记整理,不定期更新哟
基础知识准备阶段 MariaDB 版本说明
原文:Release schedule About MariaDB Server - MariaDB.org
MariaDB Foundation ensures that MariaDB Server has a steady cadence of releases. Until MariaDB Server 10.6, MariaDB Server had about one stable major release every year. Now there are multiple short term releases each year , in addition to less frequent long-term releases. The current maintained versions are: 10.4 , 10.5 , 10.6 , 10.11 (maintained for 5 years), 10.10 , 11.0 , 11.1 , 11.2 , 11.3 (maintained for one year). and the development version will be 11.4. Each stable version receives bug-fixes and security fixes periodically.
The roadmap is visible on jira.mariadb.org (login is required), along with estimated release dates.
翻译:MariaDB 基金会确保 MariaDB 服务器具有稳定的发布节奏。在 MariaDB Server 10.6 之前,MariaDB Server 每年大约有一个稳定的主要版本。现在,除了不太频繁的长期版本外,每年都有多个短期版本 释放。当前维护的版本有:10.4、10.5、10.6、10.11(维护 5 年)、10.10、11.0、11.1、11.2、 11.3 (维护 1 年)。 开发版本将为 11.4。 每个稳定版本都会定期收到错误修复和安全修复。
发布时间路线报表在 jira.mariadb.org 上可预览(需要登录),以及预计的发布日期。
小结 :MariaDB 长期稳定支持版本通常维护周期是 5 年,短期稳定支持版本通常维护周期是 1 年。
MariaDB 发行注记:Release Notes - MariaDB Knowledge Base
MariaDB 版本说明、发行注记以及维护周期整理表格如下:
点击发行注记 Release notes 链接 ,即可查看每个小版本的次带历史版本,详情请以 mariadb 官方发行注记 为主 ,可参考资料 maintenance-policy About MariaDB Server - MariaDB.org 。
友情提醒 :关于版本选择,企业或者个人生产环境 ,建议使用稳定版 (GA)。不推荐使用预览版(Preview release)和候选版(RC versions)。
mariadb 社区服务版本生命周期:Community Server
友情提醒 :EOL 表示寿命终止(end of life),文中指某一版本已停止维护;面向表达式的语言(Expression-Oriented Language)。
MariaDB 资源获取 友情提醒 :关于版本选择,企业或者个人生产环境 ,建议使用稳定版 (GA)。不推荐使用预览版(Preview release)和候选版(RC versions)。
MariaDB 在 Windows 平台提供了多种安装形式:
msi 文件:直接双击进行安装,有可视化界面,安装较为容易,但不够灵活 。
归档包(archive zip):以 zip 格式进行压缩,类似于 Linux 中的二进制包。比较灵活,只需几个命令即可安装服务和实例化 。
源码包(source package):最灵活 ,可根据需求编译安装功能,难易度最高 。
docker 形式安装:其实是在容器中安装。
Linux 或者 Unix 安装 MariaDB 有四种方式:
rpm 包安装:最为简单,但不灵活,适合初学者使用 。
二进制包(binary package):归档包(archive zip),编译好的源码包,比 rpm 包更灵活。个人认为是安装多个服务最佳选择 。
源码包(source package):最灵活 ,可根据需求编译安装功能,难易度最高 。
docker 形式安装:其实是在容器中安装。
MariaDB 所有版本下载地址,建议使用 BT 工具或者那个你们缓存过很多日本姑娘的软件 ,速度更快:
https://dlm.mariadb.com/browse/mariadb_server/
如果上面地址失效选择下面地址(点击 Show All Files 进入所有版本下载界面):
https://mariadb.com/downloads/
mariadb 社区服务版本 Community Server 参数说明
MariaDB 目前最新稳定长期支持版本:All Files - Community Server 10.11.6 (mariadb.com) ,维护到 2028 年 2 月。
你还可以通过 mariadb.org 获取(下载) mariadb server
MariaDB Server:可以选择版本、操作系统、平台,安装包类型。
MariaDB Server Repositories:可以选择操作系统发行版本、具体版本,mariadb 服务版本、仓库镜像地址(国内可选阿里云)。
Connectors:可以选择连接器的版本(支持编程语言 C、Java、Nodejs、ODBC 以及 Python),操作系统,安装包类型。
关于个人演示说明 :
采用最新长期支持稳定版本做演示,目前最新长期维护稳定版本是 mariadb-10.11.6。
Windows 平台使用 archive zip 版本。
Linux 平台,主要以 RHEL9 系列(Rocky-9)作为演示环境,安装方式主要以 Systemd 、 YUM 或者 DNF 工具 进行安装。
Windows 平台体验 mariadb-10.11.6 如果提示权限不足,请以管理员身份运行 Windows terminal、powershell 或者 cmd 。
Archive Zip 形式安装 使用 Windows terminal 打开 powershell 或者 cmd ,进入解压后的 mariadb-10.11.6-winx64.zip 目录。
1 2 unzip mariadb-10 .11.6 -winx64 .zip cd mariadb-10 .11.6 -winx64
如果没有 unzip 命令,请尝试打开 powershell 使用 Expand-Archive 命令解压。
示例解压到默认目录 :
1 Expand-Archive .\mariadb-10 .11.6 -winx64 .zip
示例解压到指定目录 D:\work\,使用参数 -DestinationPath:
1 Expand-Archive .\mariadb-10 .11.6 -winx64 .zip -DestinationPath D:\work\
如果你想指定压缩文件路径 D:\Downloads\DBMS\MariaDB\winx64\mariadb-10.11.6-winx64.zip,可以使用 -Path 参数:
1 Expand-Archive -Path D:\Downloads\DBMS\MariaDB\winx64\mariadb-10 .11.6 -winx64 .zip -DestinationPath D:\work\
可参考资料:Expand-Archive (Microsoft.PowerShell.Archive) - PowerShell | Microsoft Learn
1、初始化 mariadb
运行 mysql_install_db.exe 或者 mariadb-install-db.exe 程序进行 mariadb 安装环节,创建 my.ini 配置文件(Creating my.ini file),移除默认用户(Removing default user),创建数据库服务完成(Creation of the database was successful)。
如果提示权限不足,请以管理员身份运行 Windows terminal、powershell 或者 cmd 。
示例初始化 mariadb:
1 2 3 4 5 6 PS D:\work\mariadb-10 .11.6 -winx64 > .\bin\mysql_install_db.exeDefault data directory is D:\work\mariadb-10 .11.6 -winx64 \data Running bootstrap Creating my.ini file Removing default user Creation of the database was successful
或者执行:
1 PS D:\work\mariadb-10 .11.6 -winx64 > .\bin\mariadb-install-db .exe
2、管理 mariadb 服务
进入解压后的 .\mariadb-10.11.6-winx64\bin 或者 .\mariadb-10.11.6-winx64 运行 mysqld 或者 .\bin\mysqld 程序启动 mariadb 服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2023 -12-26 21 :55 :08 0 [Note ] Starting MariaDB 10.11 .6 -MariaDB source revision fecd78b83785d5ae96f2c6ff340375be803cd299 as process 62216 2023 -12-26 21 :55 :08 0 [Note ] InnoDB: Compressed tables use zlib 1.3 2023 -12-26 21 :55 :08 0 [Note ] InnoDB: Number of transaction pools: 1 2023 -12-26 21 :55 :08 0 [Note ] InnoDB: Using crc32 + pclmulqdq instructions2023 -12-26 21 :55 :08 0 [Note ] InnoDB: Initializing buffer pool, total size = 128.000 MiB, chunk size = 2.000 MiB2023 -12-26 21 :55 :08 0 [Note ] InnoDB: Completed initialization of buffer pool2023 -12-26 21 :55 :08 0 [Note ] InnoDB: File system buffers for log disabled (block size=4096 bytes)2023 -12-26 21 :55 :08 0 [Note ] InnoDB: End of log at LSN=46980 2023 -12-26 21 :55 :08 0 [Note ] InnoDB: 128 rollback segments are active.2023 -12-26 21 :55 :08 0 [Note ] InnoDB: Setting file './ibtmp1' size to 12.000 MiB. Physically writing the file full; Please wait ...2023 -12-26 21 :55 :08 0 [Note ] InnoDB: File './ibtmp1' size is now 12.000 MiB.2023 -12-26 21 :55 :08 0 [Note ] InnoDB: log sequence number 46980 ; transaction id 14 2023 -12-26 21 :55 :08 0 [Note ] Plugin 'FEEDBACK' is disabled.2023 -12-26 21 :55 :08 0 [Note ] InnoDB: Loading buffer pool(s) from D:\work\mariadb-10 .11.6 -winx64 \data\ib_buffer_pool2023 -12-26 21 :55 :08 0 [Note ] mysqld.exe: SSPI: using principal name 'localhost' , mech 'Negotiate' 2023 -12-26 21 :55 :08 0 [Note ] InnoDB: Buffer pool(s) load completed at 231226 21 :55 :08 2023 -12-26 21 :55 :08 0 [Note ] Server socket created on IP: '::' .2023 -12-26 21 :55 :08 0 [Note ] Server socket created on IP: '0.0.0.0' .2023 -12-26 21 :55 :08 0 [Note ] D:\work\mariadb-10 .11.6 -winx64 \bin\mysqld.exe: ready for connections.Version: '10.11.6-MariaDB' socket: '' port: 3306 mariadb.org binary distribution
还可以运行 mariadb 或者 .\bin\mariadb 程序启动 mariadb 服务。
将服务注册到 services.ms 管理:
1 .\bin\mariadbd --install
或者使用
如果 .\bin\mariadbd --install 或者 .\bin\mysqld --install 后不加服务名,默认服务名是 MySQL。
示例移除 MySQL 服务,然后重新命名为 MariaDB:
1 2 3 4 PS D:\work\mariadb-10.11.6-winx64> sc delete MySQL [SC] DeleteService 成功 PS D:\work\mariadb-10.11.6-winx64> .\bin\mariadbd --install MariaDB Service 'MariaDB' successfully installed.
通过 sc 命令管理 MariaDB 服务:
查询 MariaDB 服务:sc query mariadb;
启动 MariaDB 服务:sc start mariadb;
停止 MariaDB 服务:sc stop mariadb;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 PS D:\work\mariadb-10.11.6-winx64> sc query mariadb SERVICE_NAME: mariadb TYPE : 10 WIN32_OWN_PROCESS STATE : 1 STOPPED WIN32_EXIT_CODE : 1077 (0x435) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x0 WAIT_HINT : 0x0 PS D:\work\mariadb-10.11.6-winx64> sc start mariadb SERVICE_NAME: mariadb TYPE : 10 WIN32_OWN_PROCESS STATE : 2 START_PENDING (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN) WIN32_EXIT_CODE : 0 (0x0) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x1 WAIT_HINT : 0x0 PID : 8056 FLAGS : PS D:\work\mariadb-10.11.6-winx64> sc stop mariadb SERVICE_NAME: mariadb TYPE : 10 WIN32_OWN_PROCESS STATE : 3 STOP_PENDING (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN) WIN32_EXIT_CODE : 0 (0x0) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x4 WAIT_HINT : 0x0
当然,你还可以通过 net 命令管理 mariadb 服务:
1 2 3 4 5 6 7 PS D:\work> net start mariadbMariaDB 服务正在启动 ...... MariaDB 服务已经启动成功。 PS D:\work> net stop mariadbMariaDB 服务正在停止.. MariaDB 服务已成功停止。
Windows terminal 通过 netstat 配合 tasklist 以及 findstr 命令监控 mariadb 服务:
1 2 3 4 5 PS D:\work> netstat -ano | findstr 3306 TCP 0.0 .0.0 :3306 0.0 .0.0 :0 LISTENING 24416 TCP [::]:3306 [::]:0 LISTENING 24416 PS D:\work> tasklist | findstr 24416 mariadbd.exe 24416 Services 0 26 ,236 K
3、快捷关闭 mariadb 服务 使用 Windows terminal 打开 powershell 或者 cmd ,使用快捷键 ctrl + c 可以停止 mariadb 服务(\mariadb-10.11.6-winx64\bin\mysqld.exe: Shutdown complete)。
1 2 3 4 5 6 7 8 9 2023 -12-26 21 :57 :18 0 [Note ] console_event_handler: received CTRL_C_EVENT event, shutting down2023 -12-26 21 :57 :18 0 [Note ] D:\work\mariadb-10 .11.6 -winx64 \bin\mysqld.exe (initiated by: unknown): Normal shutdown2023 -12-26 21 :57 :18 0 [Note ] InnoDB: FTS optimize thread exiting.2023 -12-26 21 :57 :18 0 [Note ] InnoDB: Starting shutdown...2023 -12-26 21 :57 :18 0 [Note ] InnoDB: Dumping buffer pool(s) to D:\work\mariadb-10 .11.6 -winx64 \data\ib_buffer_pool2023 -12-26 21 :57 :18 0 [Note ] InnoDB: Buffer pool(s) dump completed at 231226 21 :57 :18 2023 -12-26 21 :57 :19 0 [Note ] InnoDB: Removed temporary tablespace data file: "./ibtmp1" 2023 -12-26 21 :57 :19 0 [Note ] InnoDB: Shutdown completed; log sequence number 46980 ; transaction id 15 2023 -12-26 21 :57 :19 0 [Note ] D:\work\mariadb-10 .11.6 -winx64 \bin\mysqld.exe: Shutdown complete
4、登录 mariadb monitor 字符命令行模式
如下示例,进入 .\mariadb-10.11.6-winx64 目录,执行 .\bin\mysql -uroot -p。
1 PS D:\work\mariadb-10 .11.6 -winx64 > .\bin\mysql -uroot -p
或者使用 .\bin\mariadb -uroot -p:
1 PS D:\work\mariadb-10 .11.6 -winx64 > .\bin\mariadb -uroot -p
默认 mariadb 或者 MySQL 端口号(port)均为 3306,实际生产环境可能会视实际情况而改变。比如我个人测试环境安装了多个服务,在 my.ini 或者 my.cnf 中会指定端口为 3307,达到演示目的。执行方式略有变动,指定 -P 3307,如下:
1 PS D:\work\mariadb-10 .11.6 -winx64 > .\bin\mysql -uroot -p -P 3307
或者使用:
1 PS D:\work\mariadb-10 .11.6 -winx64 > .\bin\mariadb -uroot -p -P 3307
5、初步体验
如果你使用了第三方数据库软件管理工具,注释用法如下:
演示过程,加了注释只为便于初学者理解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 MariaDB [mysql]> SELECT 1 ; + | 1 | + | 1 | + MariaDB [mysql]> EXPLAIN SELECT 1 ; + | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | + | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | + MariaDB [mysql]> SELECT version(); + | version() | + | 10.11 .6 - MariaDB | + MariaDB [mysql]> SHOW DATABASES; + | Database | + | information_schema | | mysql | | performance_schema | | sys | +
MSI 文件形式安装 由于安装过程比较容易,此处省略教程,详情请参考 Installing MariaDB MSI Packages on Windows - MariaDB Knowledge Base
Linux 平台体验 mariadb-10.11.6 友情提醒 :可直接参考 MySQL 或者 MariadDB 官方文档进行安装哟!当然,可能存在细微差别。
RHEL、Rocky、CentOS 环境 RHEL 9 使用 YUM & DNF 工具安装 mariadb :
新建 mariadb 服务仓库:
1 2 cd /etc/yum.repos.d touch MariaDB.repo
如果存在 Nvim 或者 Vim 环境,可以直接编辑 MariaDB.repo 文件进行补全:
Nvim /etc/yum.repos.d/MariaDB.repo
vim /etc/yum.repos.d/MariaDB.repo
复制如下内容到 MariaDB.repo :
1 2 3 4 5 6 7 8 9 10 # MariaDB 10.11 RedHatEnterpriseLinux repository list - created 2023-12-25 13:48 UTC # https://mariadb.org/download/ [mariadb] name = MariaDB # rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details. # baseurl = https://rpm.mariadb.org/10.11/rhel/$releasever /$basearch baseurl = https://mirrors.aliyun.com/mariadb/yum/10.11/rhel/$releasever/$basearch # gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB gpgkey = https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck = 1
国内访问 mariadb 官方镜像地址 可能较缓慢,此处采用了阿里云 的镜像地址。
安装 :
1 sudo dnf install MariaDB-server MariaDB-client
安装成功,监控 mariadb 状态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [root@localhost local]# systemctl status mariadb ● mariadb.service - MariaDB 10.11.6 database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; preset: disabled) Drop-In: /etc/systemd/system/mariadb.service.d └─migrated-from-my.cnf-settings.conf Active: active (running) since Tue 2023-12-26 15:23:02 CST; 46min ago Docs: man:mariadbd(8) https://mariadb.com/kb/en/library/systemd/ Process: 3639 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS) Process: 3640 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [> Process: 3660 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS) Main PID: 3648 (mariadbd) Status: "Taking your SQL requests now..." Tasks: 9 (limit: 10685) Memory: 216.1M CPU: 681ms CGroup: /system.slice/mariadb.service └─3648 /usr/sbin/mariadbd 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: 2023-12-26 15:23:02 0 [Note] InnoDB: File './ibtmp1' size is now 12.000MiB. 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: 2023-12-26 15:23:02 0 [Note] InnoDB: log sequence number 45502; transaction id 14 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: 2023-12-26 15:23:02 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_> 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: 2023-12-26 15:23:02 0 [Note] Plugin 'FEEDBACK' is disabled. 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: 2023-12-26 15:23:02 0 [Note] Server socket created on IP: '0.0.0.0'. 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: 2023-12-26 15:23:02 0 [Note] Server socket created on IP: '::'. 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: 2023-12-26 15:23:02 0 [Note] InnoDB: Buffer pool(s) load completed at 231226 15:23:> 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: 2023-12-26 15:23:02 0 [Note] /usr/sbin/mariadbd: ready for connections. 12月 26 15:23:02 localhost.localdomain mariadbd[3648]: Version: '10.11.6-MariaDB' socket: '/var/lib/mysql/mysql.sock' port: 3306 MariaD> 12月 26 15:23:02 localhost.localdomain systemd[1]: Started MariaDB 10.11.6 database server.
友情提醒 :初次安装 mariadb 数据库软件, root 用户密码为空,可登录终端自行更改。
修改 root@localhost 用户密码:
1 2 mariadb - uroot - p ALTER USER 'root' @'localhost' IDENTIFIED BY 'mypwd@123' ;
小结 :
优点:使用 yum 或者 dnf 工具在线,有网络,安装过程比较简单。相比离线下载 rpm 包容易上手,会自动匹配依赖包。
缺点:不灵活。
Systemd 环境 可参考资料:Installing MariaDB Binary Tarballs - MariaDB Knowledge Base
Systemd 环境安装 :
预备环境,开启 EPEL,AlmaLinux 9, Rocky Linux 9 :
1 2 dnf config-manager --set-enabled crb dnf install epel-release
安装必备依赖包 :
1 2 dnf install libaio dnf -y install ncurses-compat-libs
安装步骤:
1 2 3 4 5 6 7 8 9 groupadd mysql useradd -r -g mysql -s /bin/false mysql # 创建 mysql 用户(没有服务器主机登录权限的用户) cd /usr/local tar -zxvpf /path-to/mariadb-VERSION-OS.tar.gz ln -s mariadb-VERSION-OS mysql cd mysql ./scripts/mariadb-install-db --user=mysql chown -R root . chown -R mysql data
重点解析 :
groupadd mysql:创建 mysql 组。
useradd -r -g mysql -s /bin/false mysql: 创建 mysql 用户(没有服务器主机登录权限的用户)。
/path-to/mariadb-VERSION-OS.tar.gz:你下载的 mariadb 安装包名称所在路径。
比如我示例安装路径 /usr/local/mariadb-10.11.6-linux-systemd-x86_64.tar.gz。
./scripts/mariadb-install-db --user=mysql:指定 mysql 用户,安装 mariadb 服务。
启动 mariadb 服务 :
1 2 3 ./bin/mysqld_safe --user=mysql & or ./bin/mysqld_safe --defaults-file=~/.my.cnf --user=mysql &
配置环境变量:
1 export PATH=$PATH:/usr/local/mysql/bin/
写入配置文件,永久生效,全局则做如下设置 ,编辑:vim /etc/profile,需要 root 用户权限,或者使用 sudo vim /etc/profile:
1 2 3 4 # mysql home MYSQL_HOME=/usr/local/mysql PATH=$PATH:$MYSQL_HOME/bin export MYSQL_HOME PATH
设置 mysqld 服务使用 systemctl 进行管理:
1 2 cp support-files/mysql.server /etc/init.d/mysql.server systemctl daemon-reload
设置 mariadb 服务使用 systemctl 进行管理:
1 2 cp support-files/systemd/mariadb.service /usr/lib/systemd/system/mariadb.service systemctl daemon-reload
启动 mariadb 与停止服务:
1 2 systemctl start mariadb.service systemctl stop mariadb.service
设置 mariadb 自启:
1 systemctl enable mariadb.service
监控 mariadb 服务:
1 systemctl status mariadb.service
Source 源码包环境 本人工作中很少遇到需要使用源码包环境编译安装,扩展或者精简某些功能。此处省略教程,详情请参考 Compiling MariaDB From Source - MariaDB Knowledge Base
MariaDB 服务监控 友情提醒 ,如果条件允许,可以利用 cockpit 工具协助管理 Linux 服务器:
1 2 3 4 dnf install cockpit systemctl enable --now cockpit.socket firewall-cmd --add-service=cockpit --permanent firewall-cmd --reload
安装 cockpit 服务并设置开机自启,利用 firewalld 防火墙管理工具设置 cockpit 服务加入到 firewall-cmd 进行管理。
terminal 监控 mariadb 服务 :
1 2 3 netstat -tlunp | grep 3306 ps -aux | grep mariadb ps -ef | grep mariadb
mariadb 字符集:无法插入中文 使用标准形式安装 mariadb 服务,默认字符集是 latin1 ,插入中文,会出现如下提示:
MariaDB [test]> insert into t1 values(1,'mariadb 最新长期稳定支持版本是 10.11.6');ERROR 1366 (22007): Incorrect string value: '\xE6\x9C\x80\xE6\x96\xB0...' for column test.t1.db_names at row 1
创建表时指定字符集,成功插入中文内容:
1 2 3 4 5 6 MariaDB [test]> create table t2(id int primary key,db_names varchar (64 )) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE = utf8mb4_general_ci; MariaDB [test]> insert into t2 values (1 ,'mariadb 最新长期稳定支持版是 10.11.6' ); MariaDB [test]> select * from t2\G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * id: 1 db_names: mariadb 最新长期稳定支持版是 10.11 .6
System Variables 系统变量
Variable
MariaDB in Debian
Standard MariaDB
Notes
character_set_server
utf8mb4
latin1
Debian sets a default character set that can support emojis etc.
collation_server
utf8mb4_general_ci
latin1_swedish_ci
Windows 平台配置 my.ini,编辑 \mariadb-10.11.6-winx64\data\my.ini :
1 2 3 4 5 6 [mysqld] datadir =D:/work/mariadb-10.11 .6 -winx64/datacharacter_set_server =utf8mb4collation_server =utf8mb4_general_ci[client] plugin-dir =D:\work\mariadb-10.11 .6 -winx64/lib/plugin
设置好默认字符集以及字符集排序规则,重启 mariadb 服务即可。
查询与设置字符集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 SHOW VARIABLES LIKE 'character_set\_%' ;+ | Variable_name | Value | + | character_set_client | utf8mb3 | | character_set_connection | utf8mb3 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8mb3 | | character_set_server | latin1 | | character_set_system | utf8mb3 | + SHOW VARIABLES LIKE 'collation%' ;+ | Variable_name | Value | + | collation_connection | utf8mb3_general_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | + SET CHARACTER SET utf8mb4;SHOW VARIABLES LIKE 'character_set\_%' ;+ | Variable_name | Value | + | character_set_client | utf8mb4 | | character_set_connection | latin1 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | latin1 | | character_set_system | utf8mb3 | + SHOW VARIABLES LIKE 'collation%' ;+ | Variable_name | Value | + | collation_connection | latin1_swedish_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +
web 开发阶段 极简体验
这里所说的开发阶段,是指利用第三方工具技术与 DBMS 发行厂商发行的数据库软件进行沟通交流。
mariadb Java 连接驱动与 Java 版本、JDBC 版本对应关系:
Java Version(s)
Recommended MariaDB Connector/J Release Series
JDBC Version
Java 21, Java 17, Java 11, Java 8
MariaDB Connector/J 3.3
JDBC 4.2
Java 17, Java 11, Java 8
MariaDB Connector/J 3.0
JDBC 4.2
Java 11, Java 8
MariaDB Connector/J 2.7
JDBC 4.2
MariaDB 之 JDBC 示例:
1 Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/DB?user=root&password=myPassword" );
使用 Java JDBC 连接 mariadb,采用 Maven 或者 Gradle in IDEs 进行构建。
About MariaDB Connector/J - MariaDB Knowledge Base
Java Connector Using Maven - MariaDB Knowledge Base
Java Connector Using Gradle - MariaDB Knowledge Base
maven 配置
设置 pom.xml 配置文件,使用 mariadb 进行连接测试。maven 的镜像仓库,可以使用阿里仓库服务 (aliyun.com) 的镜像源地址。
1 2 3 4 5 <dependency > <groupId > org.mariadb.jdbc</groupId > <artifactId > mariadb-java-client</artifactId > <scope > runtime</scope > </dependency >
创建普通的 Maven 项目或者 springboot 项目,然后配置 pom.xml。如果访问、下载速度缓慢,这是正常现象,建议使用国内镜像站,比如阿里云提供的 apache-maven 安装包下载、开源镜像站 ,maven aliyun config ,更多配置可以参考阿里云云效 。
配置前建议备份一份原始 settings.xml 作为参考,apache-maven-3.8.6\conf\settings.xml 具体参考配置如下:
1 2 3 4 5 6 7 8 9 10 11 <mirrors > <mirror > <id > aliyunmaven</id > <name > aliyun maven</name > <url > https://maven.aliyun.com/repository/public/</url > <mirrorOf > *</mirrorOf > </mirror > </mirrors >
编写 Java 代码
借助编辑器 Eclipse for JavaEE 、[VSCode](Download Visual Studio Code - Mac, Linux, Windows )、Spring Tool 或者 IntelliJ IDEA 。
目的 :使用纯 JDBC 测试,或者 ORM 框架 mybatis、hibernate、或者 JPA,最终达到对数据库进行最基本的沟通交流(增删改查)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 package com.example.demo.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class TestConnMariaDB { public static void main (String[] args) throws ClassNotFoundException, SQLException { TestSQLConnMariadb(); } private static final Logger log = LoggerFactory.getLogger(TestSQLConnMariadb.class); static Connection conn = null ; static PreparedStatement ps = null ; static ResultSet rs = null ; private static void TestSQLConnMariadb () throws SQLException, ClassNotFoundException { try { Class.forName("org.mariadb.jdbc.Driver" ); String url = "jdbc:mariadb://127.0.0.1:3306/TEST?useUnicode=true&characterEncoding=utf-8" ; String username = "root" ; String password = "mypwd@123" ; conn = DriverManager.getConnection(url, username, password); if (null != conn) { log.info("connect database success..." ); }else { log.error("connect database failed..." ); } String sql = "SELECT * FROM STUDY" ; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { System.out.println("ID:" + rs.getInt("ID" )); System.out.println("姓名:" + rs.getString("NAMES" )); } } finally { rs.close(); ps.close(); conn.close(); } } }
Springboot 之 JPA 如果 SpringMVC(模型 - 视图 - 控制器) 按标准书写:数据持久层、数据业务层、数据展示层。注意了 ,个人演示环境,省略了业务逻辑层,直接调用 repository 获取数据。
配置 pom.xml :
如果不清楚需要引入哪些 jar 包,可以前往 Maven Repository 搜索。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <dependency > <groupId > org.mariadb.jdbc</groupId > <artifactId > mariadb-java-client</artifactId > <version > 3.1.4</version > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-jdbc</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-thymeleaf</artifactId > </dependency >
配置 properties 文件 :
application.properties
1 spring.profiles.active =dev
application-dev.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 server.port =8081 spring.application.name =springboot-crud spring.thymeleaf.prefix =classpath:/templates/templates.html/ spring.thymeleaf.suffix =.html spring.datasource.type =com.zaxxer.hikari.HikariDataSource spring.datasource.driver-class-name =org.mariadb.jdbc.Driver spring.datasource.url =jdbc:mariadb://127.0.0.1:3306/world?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true spring.datasource.username =root spring.datasource.password =mypwd@123 spring.datasource.hikari.connection-timeout =30000 spring.datasource.hikari.minimum-idle =5 spring.datasource.hikari.maximum-pool-size =20 spring.datasource.hikari.auto-commit =true spring.datasource.hikari.idle-timeout =600000 spring.datasource.hikari.pool-name =DataSourceHikariCP spring.datasource.hikari.max-lifetime =1800000 spring.datasource.hikari.connection-test-query =SELECT 1 spring.jpa.show-sql =true spring.jpa.hibernate.ddl-auto =none spring.jpa.hibernate.naming.physical-strategy =org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
编写 CityRepository 接口类 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.example.repository;import java.util.List;import java.util.Map;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.Query;import com.example.entity.City;import com.example.entity.bo.CityBo;public interface CityRepository extends JpaRepository <City, Object>{ @Query(value = "select c.ID, c.Name, c.CountryCode ,c.District ,c.Population from world.city c;",nativeQuery = true) public List<City> findAll () ; @Query(value = "select c.ID, c.Name, c.CountryCode from world.city c;",nativeQuery = true) public List<Map<String, Object>> findAll (City city) ; @Query(value = "select c.ID, c.Name, c.CountryCode from world.city c;",nativeQuery = true) public List<Map<CityBo, Object>> findCityBo () ; @Query(value = "select c.Name, c.CountryCode from world.city c;",nativeQuery = true) public List<Map<CityBo, Object>> findCity () ;
编写 CityController 类测试访问 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 package com.example.controller;import java.util.List;import java.util.Map;import java.util.Optional;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import com.example.entity.City;import com.example.entity.bo.CityBo;import com.example.repository.CityRepository;import com.example.service.impl.CityServiceImpl;import lombok.extern.slf4j.Slf4j;@RestController @RequestMapping("/world") @Slf4j public class CityController { @Autowired private CityServiceImpl cityService; @GetMapping("/city/findCityFive") public List<City> findCityFive () { return repository.findAll().subList(0 , 5 ); }
Thymeleaf 页面交互 使用 Thymeleaf 与数据展示层(controller)进行交互。
编写 ThymeleafController 类 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.example.controller;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import com.example.entity.bo.CityBo;import com.example.repository.CityRepository;import jakarta.servlet.http.HttpServletRequest;import lombok.extern.slf4j.Slf4j;@Controller @RequestMapping(value = "/thymeleaf") @Slf4j public class ThymeleafController { @Autowired private CityRepository repository; @GetMapping("/findCity") public Object findCity (Model model) { List<Map<CityBo, Object>> list = repository.findCity().subList(5 , 15 ); model.addAttribute("list" , list); return "index" ; } }
编写 index.html 页面 :
使用如下路径方式构建 resources\templates\templates.html\index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <!DOCTYPE html > <html xmlns:th ="http://www.thymeleaf.org" > <html lang ="en" > <head > <meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" /> <title > 首页</title > </head > <body > maps大小:<span th:text ="${#maps.size(list)}" > </span > <br /> maps是否为空:<span th:text ="${#maps.isEmpty(list)}" > </span > <br /> containsValue:<span th:text ="${#maps.containsValue(list,value)}" > </span > <br /> <div > <h1 > thymeleaf 迭代列表 </h1 > <table width ="100%" border ="0" cellspacing ="1" cellpadding ="4" bgcolor ="#cccccc" class ="tabtop13" align ="center" > <thead > <tr > <th > 城市名称</th > <th > 国家代码</th > </tr > </thead > <tbody > <tr th:each ="cityList: ${list}" > <td th:text ="${cityList.Name}" > name</td > <td th:text ="${cityList.countryCode}" > countryCode</td > </tr > </tbody > </table > </div > </body > </html >
参考资料
如果你能看到这篇博文笔记,希望对你的工作和学习有些许帮助。
—END—