# CentOS 7/8安装MariaDB(MySQL兼容分支)
sudo yum install mariadb-server mariadb
sudo systemctl start mariadb
sudo systemctl enable mariadb
# 安全配置
sudo mysql_secure_installation
# CentOS 7/8安装PostgreSQL
sudo yum install postgresql-server postgresql-contrib
sudo postgresql-setup initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql
修改默认端口(可选但推荐):
# MySQL/MariaDB修改/etc/my.cnf
[mysqld]
port = 3307
# PostgreSQL修改/var/lib/pgsql/data/postgresql.conf
port = 5433
限制访问IP:
GRANT ALL ON db.* TO 'user'@'192.168.1.%' IDENTIFIED BY 'password';
pg_hba.conf
文件定期备份:
# MySQL备份
mysqldump -u root -p --all-databases > full_backup.sql
# PostgreSQL备份
pg_dumpall -U postgres > full_backup.sql
/etc/my.cnf
中的缓冲池大小:
ini
innodb_buffer_pool_size = 1G # 根据服务器内存调整
ini
query_cache_size = 64M
query_cache_type = 1
postgresql.conf
:
ini
shared_buffers = 1GB # 25% of total RAM
effective_cache_size = 3GB # 50-75% of total RAM
work_mem = 16MB # for complex sorts
PHP连接MySQL:
$conn = new mysqli("localhost", "username", "password", "database");
Python连接PostgreSQL:
import psycopg2
conn = psycopg2.connect("dbname=test user=postgres")
安装监控工具:
mytop
或 pt-mysql-summary
pg_top
或 pgAdmin
定期维护:
# MySQL表维护
mysqlcheck -u root -p --auto-repair --optimize --all-databases
# PostgreSQL维护
vacuumdb --all --analyze -U postgres
根据您的具体应用需求、数据结构和流量预期选择合适的数据库解决方案。对于大多数传统Web应用,MySQL/MariaDB是良好的起点;对于需要更复杂数据处理的应用,PostgreSQL可能更合适;而需要处理大量非结构化数据时,可考虑MongoDB。