vi

Cài MySQL/MariaDB trên VPS: Hướng Dẫn Database

MySQL và MariaDB là hai database server phổ biến nhất cho web application. Cài đặt đúng cách trên VPS offshore AnubizHost giúp bạn có database nhanh, bảo mật và đáng tin cậy. Hướng dẫn này bao gồm cài đặt, cấu hình bảo mật và tối ưu hiệu suất.

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

Cài Đặt MySQL/MariaDB

Chọn MySQL hoặc MariaDB (tương thích hoàn toàn, MariaDB thường nhanh hơn):

# Cài MariaDB (khuyên dùng)
apt update
apt install -y mariadb-server mariadb-client

# Hoặc cài MySQL
apt install -y mysql-server mysql-client

# Bảo mật cài đặt ban đầu
mysql_secure_installation

Trong quá trình secure installation, chọn:

  • Set root password: Yes
  • Remove anonymous users: Yes
  • Disallow root login remotely: Yes
  • Remove test database: Yes
  • Reload privilege tables: Yes

Tạo Database và Quản Lý User

Quản lý database và user đúng cách:

mysql -u root -p
-- Tạo database với charset UTF8
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Tạo user chỉ có quyền trên database cụ thể
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPassword!123';
GRANT ALL PRIVILEGES ON myapp.* TO 'appuser'@'localhost';

-- Tạo user read-only để backup
CREATE USER 'backupuser'@'localhost' IDENTIFIED BY 'BackupPass!456';
GRANT SELECT, LOCK TABLES, SHOW VIEW ON *.* TO 'backupuser'@'localhost';

FLUSH PRIVILEGES;
SHOW GRANTS FOR 'appuser'@'localhost';

Tối Ưu Hiệu Suất MySQL

Cấu hình MySQL trong /etc/mysql/mariadb.conf.d/50-server.cnf:

[mysqld]
# InnoDB buffer pool (set to 70-80% RAM cho DB server)
innodb_buffer_pool_size = 1G

# Query cache (MariaDB)
query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 2M

# Connections
max_connections = 150
thread_cache_size = 10

# Logging slow queries
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2

# Temporary tables
tmp_table_size = 64M
max_heap_table_size = 64M
systemctl restart mysql

Backup và Restore Database

Script backup tự động với nhiều options:

nano /usr/local/bin/mysql-backup.sh
#!/bin/bash
BACKUP_DIR="/var/backups/mysql"
DATE=$(date +%Y%m%d_%H%M%S)
USER="backupuser"
PASS="BackupPass!456"

mkdir -p $BACKUP_DIR/$DATE

# Backup từng database riêng
for DB in $(mysql -u$USER -p$PASS -e "SHOW DATABASES;" | grep -v "Database|sys|information_schema|performance_schema"); do
    mysqldump -u$USER -p$PASS --single-transaction --routines --triggers $DB | gzip > $BACKUP_DIR/$DATE/$DB.sql.gz
    echo "Backed up: $DB"
done

# Xóa backup cũ hơn 30 ngày
find $BACKUP_DIR -type d -mtime +30 -exec rm -rf {} +

Restore:

gunzip -c backup.sql.gz | mysql -u root -p database_name

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

Ready to get started?

Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.

Anubiz Chat AI

Online