هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
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.
| Item | RHEL / Rocky / Alma / Fedora | Ubuntu / Debian |
|---|---|---|
| MariaDB package | mariadb-server | mariadb-server |
| MySQL package | mysql-server (RHEL 9: 8.0; RHEL 10: 8.4) | mysql-server |
| Service | mariadb / mysqld | mariadb / 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 |
| Port | 3306/tcp | 3306/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.
Install on RHEL, Rocky, Alma and Fedora
For MySQL: dnf -y install mysql-server and systemctl enable --now mysqld.
Install on Ubuntu and Debian
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:
Create the app database and user
Use utf8mb4 so all languages and emoji work. Give the app user rights only on its own database:
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();'First configuration: network, memory and logs
Write your settings in your own file, so package updates do not change them:
[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.)
| Setting | Why |
|---|---|
bind-address | Listen only on the private IP (or 127.0.0.1 when the app is on the same server). |
innodb_buffer_pool_size | The most important memory setting: about 50–70 % of RAM on a database-only server. |
max_connections | Enough for all app workers; each connection uses memory. |
slow_query_log + long_query_time | Find slow queries (over 1 second). |
skip-name-resolve | No 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 tcpBackups
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.
Daily use: the commands and the real uses
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 --checkReal 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.
Make it safe
Security checklist:
- Run
mariadb-secure-installation; root only through the local socket. - Never open 3306 to the internet;
bind-addresson 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.
Update and remove
dnf -y upgrade mariadb-server
apt-get install --only-upgrade -y mariadb-server
mariadb-upgradeBefore 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-serverFix common problems
| Problem | Cause | Fix |
|---|---|---|
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 socket | The service is stopped. | systemctl status mariadb; journalctl -u mariadb -n 50. |
Can't connect to MySQL server on '10.0.1.21' from the app | bind-address or firewall. | Check ss -ltnp | grep 3306 and the firewall rule. |
Too many connections | max_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 change | Typo or a setting the version does not know. | journalctl -u mariadb; remove the bad line. |
Next steps
- Quick how-tos: Back up and restore MySQL, Reset a lost root password, Who is connected, Find slow queries.
- Command page: mysql — client options and examples.
- MySQL and the other data services at work: The web app database.
- Another database: Install and set up PostgreSQL. A cache in front: Install and set up Redis.
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.