Sometimes you can’t install MariaDB with apt/yum, or you want every node in a database cluster running exactly the same MariaDB version. My motivation this time was building a MariaDB cluster with identical versions across nodes for easier management.
Prerequisites
First, my environment. Software can be finicky — different environments produce weird problems, and my configuration is tuned for my hardware and may not suit yours. So here’s what I’m running:
- OS: Ubuntu 24.04.1 LTS
- CPU: Intel(R) Xeon(R) Gold 6326 CPU @ 2.90GHz
- RAM: 32 GB
- SWAP: 8 GB
- Download MariaDB: https://mariadb.org/download/ (do not pick an RC release!)
I downloaded the 11.4.4 Linux x86_64 systemd tarball, which arrives as mariadb-11.4.4-linux-systemd-x86_64.tar.gz — replace with your own filename throughout.
Unpack and install
Unpack into /usr/local (or wherever you prefer), then enter the directory:
sudo tar -zxvf mariadb-11.4.4-linux-systemd-x86_64.tar.gz -C /usr/local
cd /usr/local/mariadb-11.4.4-linux-systemd-x86_64
Read the install notes:
cat INSTALL-BINARY
The doc explains everything clearly — my steps below basically follow it.
Configure the environment and install
# 创建用户和组
groupadd mysql
useradd -g mysql mysql
# 注意这里的目录,我是解压到了 /usr/local,替换成你的目录
cd /usr/local
# 创建软连接,方便多版本转换,注意你自己的路径和文件夹名称
ln -s /usr/local/mariadb-11.4.4-linux-systemd-x86_64 /usr/local/mysql
# 进入软连接的目录,就是程序的主目录
cd mysql
# 修改所属权限
chown -R mysql .
chgrp -R mysql .
Configure the database: /etc/my.cnf
One thing to note here: don’t rush ahead. Some parameters must be set before the database is initialized. So pause, decide what you need to configure, and write your /etc/my.cnf first. Here’s mine for reference — it may not fit your environment:
[mysqld]
lower_case_table_names=1
port=3306
innodb-page-size=65536
character-set-server=utf8mb4
## * Fine Tuning
max_connections = 300 ## 最大连接数
open_files_limit = 1048576 ## 打开文件数限制
## * Query Cache Configuration
query_cache_type = ON
query_cache_size = 32M
query_cache_limit = 1G
performance_schema = ON
table_open_cache = 3000
## * InnoDB
default-storage-engine = innodb
innodb_file_per_table = 1
innodb_buffer_pool_size = 26G ## InnoDB存储引擎的缓冲区 70%
innodb_buffer_pool_instances = 1
innodb_flush_log_at_trx_commit = 0
innodb_log_file_size = 5G
innodb_log_buffer_size = 512M
innodb_read_io_threads=16
innodb_write_io_threads=16
key_buffer_size = 10M
skip-name-resolve
max_heap_table_size = 24G
(The comments are in Chinese: max connections / open files limit / InnoDB buffer pool, 70% of RAM.)
Initialize the database
# 初始化数据库
scripts/mysql_install_db --user=mysql
# 修改目录权限
chown -R root .
chown -R mysql data
# 设置环境变量
vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin/
# 注册为服务
cp support-files/systemd/mariadb.service /usr/lib/systemd/system/mariadb.service
mkdir /etc/systemd/system/mariadb.service.d/
cat > /etc/systemd/system/mariadb.service.d/datadir.conf <<EOF
[Service]
ReadWritePaths=/usr/local/mysql/data
EOF
# 重新加载服务列表
systemctl daemon-reload
# 启动 mariadb 服务
systemctl start mariadb.service
# 查看 mariadb 服务状态
systemctl status mariadb.service
# 启用 mariadb 服务开机自启
systemctl enable mariadb.service
Log in and test
# 登录数据库
mysql -uroot
MariaDB [(none)]> use mysql;
# 设置密码
MariaDB [mysql]> SET password=PASSWORD('newpassward');
MariaDB [mysql]> FLUSH PRIVILEGES;
# 如果需要远程访问还需要下面的命令
MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'newpassward';
That completes the binary install of MariaDB.
