متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 15MySQL / MariaDB · Data

Install and set up MySQL or MariaDB

MySQL and MariaDB run most web apps: WordPress, WooCommerce, Magento, Laravel, Nextcloud and many company systems. MariaDB started as a copy of MySQL and works almost the same way. This guide installs MariaDB (or MySQL) on RHEL and Ubuntu, secures the first install, creates the app database and user, tunes memory and the slow query log, opens it safely to app servers, and makes backups you can restore.

  • 12 الدروس
  • 10 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

15.1

Know it before you install

One server holds many databases. A user is always user@host: 'shop'@'10.0.1.%' and 'shop'@'localhost' are two different users with their own passwords and rights.

ItemRHEL / Rocky / Alma / FedoraUbuntu / Debian
MariaDB packagemariadb-servermariadb-server
MySQL packagemysql-server (RHEL 9: 8.0; RHEL 10: 8.4)mysql-server
Servicemariadb / mysqldmariadb / mysql
Config/etc/my.cnf.d/*.cnf/etc/mysql/mariadb.conf.d/50-server.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf
Data/var/lib/mysql//var/lib/mysql/
Logs/var/log/mariadb/ or /var/log/mysql//var/log/mysql/ and journalctl
Port3306/tcp3306/tcp

Choose one: MariaDB is the default in RHEL and Debian; MySQL 8 when the app needs MySQL features or you use MySQL in the cloud (Amazon RDS, Azure). The commands below are for MariaDB; MySQL notes are added where they differ.

15.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install mariadb-server
systemctl enable --now mariadb
mariadb --version

For MySQL: dnf -y install mysql-server and systemctl enable --now mysqld.

15.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y mariadb-server
systemctl status mariadb --no-pager

For MySQL: apt-get install -y mysql-server (the service is mysql).

15.4

Secure the first install

Run the security script once. It removes anonymous users and the test database and stops remote root login:

mariadb-secure-installation
Switch to unix_socket authentication [Y/n] Y
Change the root password? [Y/n] n
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

(MySQL: mysql_secure_installation.) With unix_socket authentication, the database user root can log in only as the Linux user root, with no password:

mariadb

الأوامر في هذا الدرس

15.5

Create the app database and user

Use utf8mb4 so all languages and emoji work. Give the app user rights only on its own database:

SQL
CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'shop'@'localhost' IDENTIFIED BY 'change-me-long-random';
CREATE USER 'shop'@'10.0.1.%'  IDENTIFIED BY 'change-me-long-random';
GRANT ALL PRIVILEGES ON shop.* TO 'shop'@'localhost', 'shop'@'10.0.1.%';
FLUSH PRIVILEGES;
SHOW GRANTS FOR 'shop'@'10.0.1.%';

Test it:

mariadb -u shop -p -h 127.0.0.1 shop -e 'SELECT DATABASE(), CURRENT_USER();'

الأوامر في هذا الدرس

15.6

First configuration: network, memory and logs

Write your settings in your own file, so package updates do not change them:

/etc/my.cnf.d/90-server.cnf
[mysqld]
bind-address            = 10.0.1.21
max_connections         = 200
innodb_buffer_pool_size = 4G
innodb_log_file_size    = 512M
character-set-server    = utf8mb4
collation-server        = utf8mb4_unicode_ci
slow_query_log          = ON
slow_query_log_file     = /var/log/mariadb/slow.log
long_query_time         = 1
skip-name-resolve

(On Ubuntu put it in /etc/mysql/mariadb.conf.d/90-server.cnf and use /var/log/mysql/slow.log.)

SettingWhy
bind-addressListen only on the private IP (or 127.0.0.1 when the app is on the same server).
innodb_buffer_pool_sizeThe most important memory setting: about 50–70 % of RAM on a database-only server.
max_connectionsEnough for all app workers; each connection uses memory.
slow_query_log + long_query_timeFind slow queries (over 1 second).
skip-name-resolveNo DNS lookups at login (faster). Users must then use IPs, not host names.

Restart and open the firewall only for the app subnet:

systemctl restart mariadb
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" service name="mysql" accept'
firewall-cmd --reload
ufw allow from 10.0.1.0/24 to any port 3306 proto tcp

15.7

Backups

A consistent dump of InnoDB tables, without locking the app:

mariadb-dump --single-transaction --routines --triggers --events shop | gzip > /backup/shop-$(date +%F).sql.gz
mariadb-dump --single-transaction --all-databases | gzip > /backup/all-$(date +%F).sql.gz
gunzip < /backup/shop-2026-11-16.sql.gz | mariadb shop_test

(MySQL: mysqldump and mysql.) For big databases use a physical backup: mariadb-backup (MariaDB) or Percona XtraBackup (MySQL). Copy backups to another place and test the restore every month.

15.8

Daily use: the commands and the real uses

SQL
SHOW DATABASES;
SHOW PROCESSLIST;
SHOW FULL PROCESSLIST;
KILL 1234;
SELECT table_schema, ROUND(SUM(data_length+index_length)/1024/1024) AS mb
  FROM information_schema.tables GROUP BY table_schema;
SHOW ENGINE INNODB STATUS\G
EXPLAIN SELECT * FROM orders WHERE customer_id = 42;
mysqladmin status
tail -f /var/log/mariadb/slow.log
mariadb-check --all-databases --check

Real uses at work:

  • Databases for WordPress/WooCommerce, ERP and internal apps.
  • Find slow pages: slow query log → EXPLAIN → add an index.
  • Replication (primary → replica) for read scaling and a warm standby.

الأوامر في هذا الدرس

15.9

Make it safe

Security checklist:

  • Run mariadb-secure-installation; root only through the local socket.
  • Never open 3306 to the internet; bind-address on a private IP; firewall for the app subnet only.
  • One user per app, rights only on its database, host limited ('app'@'10.0.1.%', never '%').
  • Long random passwords; rotate them when people leave.
  • TLS for connections between servers (require_secure_transport = ON).
  • Encrypted backups in another place, with restore tests; keep the server updated.

15.10

Update and remove

dnf -y upgrade mariadb-server
apt-get install --only-upgrade -y mariadb-server
mariadb-upgrade

Before a major upgrade (for example 10.11 → 11.4, or MySQL 8.0 → 8.4) make a full backup and read the upgrade notes. Remove (this does not delete /var/lib/mysql on RHEL — delete it yourself only when you are sure):

dnf -y remove mariadb-server
apt-get purge -y mariadb-server

الأوامر في هذا الدرس

15.11

Fix common problems

ProblemCauseFix
Access denied for user 'root'@'localhost'root uses socket auth.sudo mariadb (as Linux root), not mariadb -u root -p.
Access denied for user 'shop'@'10.0.1.15'No user for that host, or wrong password.SELECT user, host FROM mysql.user; — create 'shop'@'10.0.1.%'.
Can't connect to local server through socketThe service is stopped.systemctl status mariadb; journalctl -u mariadb -n 50.
Can't connect to MySQL server on '10.0.1.21' from the appbind-address or firewall.Check ss -ltnp | grep 3306 and the firewall rule.
Too many connectionsmax_connections reached, or the app leaks connections.SHOW PROCESSLIST; fix the app pool; raise the limit.
Incorrect string value: '\xF0\x9F…'The table is not utf8mb4.ALTER TABLE t CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Server does not start after a config changeTypo or a setting the version does not know.journalctl -u mariadb; remove the bad line.

15.12

Next steps

1Are 'shop'@'localhost' and 'shop'@'10.0.1.%' the same user?

No. They are two users, each with its own password and rights.

2Which setting is the most important for MySQL/MariaDB speed?

innodb_buffer_pool_size — about 50–70 % of RAM on a database-only server.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل