数据库服务器配置指南
AI生成:本文由人工智能辅助生成,内容已经过人工审核。
本文详细介绍了MySQL、PostgreSQL和Redis数据库的安装、配置、优化、备份和安全加固方法,帮助你构建高性能、安全可靠的数据库服务器。
1. MySQL数据库配置 MySQL是目前最流行的开源关系型数据库之一,下面详细介绍MySQL的安装配置和优化方法。
1.1 MySQL安装 Ubuntu/Debian安装 1 2 3 4 5 6 7 8 apt update apt install -y mysql-server mysql_secure_installation
CentOS/RHEL安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm yum install -y mysql-community-server systemctl start mysqld systemctl enable mysqld grep 'temporary password' /var/log/mysqld.log mysql_secure_installation
1.2 MySQL基础配置 MySQL的主配置文件通常位于:
Ubuntu/Debian: /etc/mysql/mysql.conf.d/mysqld.cnf
CentOS/RHEL: /etc/my.cnf
基本配置示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [mysqld] user = mysqlpid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockdatadir = /var/lib/mysqllog-error = /var/log/mysql/error.logcharacter-set-server = utf8mb4collation-server = utf8mb4_unicode_cimax_connections = 150 max_allowed_packet = 16 Mwait_timeout = 600 interactive_timeout = 600 innodb_buffer_pool_size = 1 Ginnodb_log_file_size = 256 Minnodb_flush_log_at_trx_commit = 1 innodb_flush_method = O_DIRECTslow_query_log = 1 slow_query_log_file = /var/log/mysql/mysql-slow.loglong_query_time = 2
应用配置并重启MySQL:
1 2 3 systemctl restart mysql systemctl restart mysqld
1.3 MySQL用户和权限管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 mysql - u root - p CREATE USER 'newuser' @'localhost' IDENTIFIED BY 'password' ;CREATE USER 'newuser' @'%' IDENTIFIED BY 'password' ; GRANT ALL PRIVILEGES ON database_name.* TO 'newuser' @'localhost' ;GRANT SELECT , INSERT , UPDATE ON database_name.* TO 'newuser' @'%' ;FLUSH PRIVILEGES; SHOW GRANTS FOR 'newuser' @'localhost' ;REVOKE ALL PRIVILEGES ON database_name.* FROM 'newuser' @'localhost' ;DROP USER 'newuser' @'localhost' ;
1.4 MySQL性能优化 内存配置优化 1 2 3 4 5 6 7 8 9 10 11 12 innodb_buffer_pool_size = 2 G innodb_log_file_size = 512 M innodb_buffer_pool_instances = 4 query_cache_size = 64 M query_cache_type = 1 max_connections = 200 thread_cache_size = 16
存储引擎优化 1 2 3 4 5 innodb_flush_log_at_trx_commit = 2 innodb_flush_method = O_DIRECT innodb_file_per_table = 1 innodb_io_capacity = 1000
查询优化 1 2 3 4 5 6 7 8 CREATE INDEX idx_name ON table_name (column_name);OPTIMIZE TABLE table_name; ANALYZE TABLE table_name;
1.5 MySQL备份与恢复 使用mysqldump备份 1 2 3 4 5 6 7 8 9 10 11 mysqldump -u root -p database_name > database_name.sql mysqldump -u root -p --databases db1 db2 > databases.sql mysqldump -u root -p --all-databases > all_databases.sql mysqldump -u root -p --no-data database_name > schema.sql
恢复数据 1 2 3 4 5 mysql -u root -p database_name < database_name.sql mysql -u root -p < databases.sql
使用Percona XtraBackup进行物理备份 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 apt install -y percona-xtrabackup-80 yum install -y percona-xtrabackup-80 xtrabackup --backup --target-dir=/backup/full xtrabackup --prepare --target-dir=/backup/full systemctl stop mysql rm -rf /var/lib/mysql/*xtrabackup --copy-back --target-dir=/backup/full chown -R mysql:mysql /var/lib/mysqlsystemctl start mysql
1.6 MySQL监控与维护 设置监控脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #!/bin/bash mysqladmin -u monitor_user -p'password' ping &>/dev/null if [ $? -ne 0 ]; then echo "MySQL is down!" | mail -s "MySQL Alert" admin@example.com systemctl restart mysql else echo "MySQL is running" fi CONN_COUNT=$(mysqladmin -u monitor_user -p'password' status | awk '{print $4}' ) if [ $CONN_COUNT -gt 100 ]; then echo "High connection count: $CONN_COUNT " | mail -s "MySQL Connection Alert" admin@example.com fi SLOW_QUERIES=$(mysqladmin -u monitor_user -p'password' extended-status | grep "Slow_queries" | awk '{print $4}' ) if [ $SLOW_QUERIES -gt 10 ]; then echo "Slow queries detected: $SLOW_QUERIES " | mail -s "MySQL Performance Alert" admin@example.com fi
设置定时任务:
1 2 chmod +x /usr/local/bin/mysql_monitor.shecho "*/5 * * * * root /usr/local/bin/mysql_monitor.sh" > /etc/cron.d/mysql_monitor
MySQL日志轮转 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 cat > /etc/logrotate.d/mysql << EOF /var/log/mysql/*.log { daily rotate 7 missingok create 640 mysql adm compress delaycompress sharedscripts postrotate if [ -x /usr/bin/mysqladmin ] && [ -r /etc/mysql/debian.cnf ]; then /usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf flush-logs fi endscript } EOF
1.7 MySQL安全加固 1 2 3 4 5 6 7 8 9 10 11 12 13 14 local_infile = 0 max_connect_errors = 10 ssl_cert = /etc/mysql/server-cert.pemssl_key = /etc/mysql/server-key.pemssl_ca = /etc/mysql/ca.pemaudit_log = FORCE_PLUS_PERMANENTaudit_log_file = /var/log/mysql/audit.log
配置防火墙只允许特定IP访问MySQL:
1 2 3 4 5 6 ufw allow from 192.168.1.0/24 to any port 3306 firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.1.0/24 port port=3306 protocol=tcp accept' firewall-cmd --reload
2. PostgreSQL数据库配置 PostgreSQL是一个功能强大的开源关系型数据库系统,以其可靠性、稳定性和丰富的功能著称。
2.1 PostgreSQL安装 Ubuntu/Debian安装 1 2 3 4 5 6 7 8 apt update apt install -y postgresql postgresql-contrib sudo -u postgres psql
CentOS/RHEL安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm dnf -qy module disable postgresql dnf install -y postgresql13-server postgresql13-contrib /usr/pgsql-13/bin/postgresql-13-setup initdb systemctl enable postgresql-13 systemctl start postgresql-13
2.2 PostgreSQL基础配置 PostgreSQL的主要配置文件位于数据目录中:
Ubuntu/Debian: /etc/postgresql/13/main/postgresql.conf
和pg_hba.conf
CentOS/RHEL: /var/lib/pgsql/13/data/postgresql.conf
和pg_hba.conf
基本配置:
1 2 3 4 5 6 7 8 9 10 11 listen_addresses = 'localhost' max_connections = 100 shared_buffers = 1 GB work_mem = 16 MB maintenance_work_mem = 256 MB effective_cache_size = 3 GB wal_buffers = 16 MB synchronous_commit = off max_wal_size = 1 GB checkpoint_timeout = 15 min
客户端认证配置 (pg_hba.conf):
1 2 3 4 5 6 7 8 9 # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5 # Allow remote connections from trusted network: host all all 192.168.1.0/24 md5
应用配置更改:
1 2 3 systemctl restart postgresql systemctl restart postgresql-13
2.3 PostgreSQL用户和数据库管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 sudo - u postgres psql CREATE USER dbuser WITH ENCRYPTED PASSWORD 'password' ;CREATE DATABASE mydb;GRANT ALL PRIVILEGES ON DATABASE mydb TO dbuser;\c mydb CREATE SCHEMA IF NOT EXISTS myschema AUTHORIZATION dbuser;ALTER DATABASE mydb OWNER TO dbuser;REVOKE ALL PRIVILEGES ON DATABASE mydb FROM dbuser;DROP USER dbuser;
2.4 PostgreSQL性能优化 内存配置 1 2 3 4 5 6 shared_buffers = 1 GB work_mem = 16 MB maintenance_work_mem = 256 MB effective_cache_size = 3 GB temp_buffers = 16 MB
写入性能 1 2 3 4 5 6 wal_buffers = 16 MB checkpoint_timeout = 15 min max_wal_size = 1 GB checkpoint_completion_target = 0.9 synchronous_commit = off
查询优化 1 2 3 4 5 6 7 8 9 10 11 ANALYZE table_name; CREATE INDEX idx_name ON table_name (column_name);EXPLAIN ANALYZE SELECT * FROM table_name WHERE condition ; ALTER TABLE table_name SET STATISTICS 1000 ;
2.5 PostgreSQL备份与恢复 使用pg_dump和pg_dumpall备份 1 2 3 4 5 6 7 8 9 10 11 pg_dump -U postgres -d mydb -f mydb_backup.sql pg_dump -U postgres -d mydb | gzip > mydb_backup.sql.gz pg_dump -U postgres -d mydb -Fc -f mydb_backup.dump pg_dumpall -U postgres > all_databases.sql
恢复数据 1 2 3 4 5 6 7 8 psql -U postgres -d mydb < mydb_backup.sql gunzip -c mydb_backup.sql.gz | psql -U postgres -d mydb pg_restore -U postgres -d mydb mydb_backup.dump
使用pg_basebackup物理备份 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 pg_basebackup -h localhost -U postgres -D /backup/postgresql -Fp -Xs -P systemctl stop postgresql rm -rf /var/lib/postgresql/13/main/*cp -R /backup/postgresql/* /var/lib/postgresql/13/main/chown -R postgres:postgres /var/lib/postgresql/13/mainchmod 700 /var/lib/postgresql/13/mainsystemctl start postgresql
2.6 PostgreSQL监控和维护 设置监控脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #!/bin/bash sudo -u postgres psql -c "SELECT 1" >/dev/null 2>&1if [ $? -ne 0 ]; then echo "PostgreSQL is down!" | mail -s "PostgreSQL Alert" admin@example.com systemctl restart postgresql else echo "PostgreSQL is running" fi CONN_COUNT=$(sudo -u postgres psql -t -c "SELECT count(*) FROM pg_stat_activity" ) if [ $CONN_COUNT -gt 50 ]; then echo "High connection count: $CONN_COUNT " | mail -s "PostgreSQL Connection Alert" admin@example.com fi sudo -u postgres psql -t -c "SELECT pid, now() - query_start AS duration, query FROM pg_stat_activity WHERE state = 'active' AND now() - query_start > '5 minutes'::interval" | while read linedo if [ ! -z "$line " ]; then echo "Long running query: $line " | mail -s "PostgreSQL Performance Alert" admin@example.com fi done
设置定时任务:
1 2 chmod +x /usr/local/bin/pgsql_monitor.shecho "*/5 * * * * root /usr/local/bin/pgsql_monitor.sh" > /etc/cron.d/pgsql_monitor
维护任务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 VACUUM; ANALYZE; VACUUM FULL ; ALTER SYSTEM SET autovacuum = on ;ALTER SYSTEM SET autovacuum_vacuum_threshold = 50 ;ALTER SYSTEM SET autovacuum_analyze_threshold = 50 ;ALTER SYSTEM SET autovacuum_vacuum_scale_factor = 0.1 ;ALTER SYSTEM SET autovacuum_analyze_scale_factor = 0.05 ;SELECT pg_reload_conf();
2.7 PostgreSQL安全加固 1 2 3 4 5 6 7 8 ssl = on ssl_cert_file = 'server.crt' ssl_key_file = 'server.key' password_encryption = scram-sha-256 log_connections = on log_disconnections = on log_statement = 'ddl'
配置防火墙只允许指定IP访问PostgreSQL:
1 2 3 4 5 6 ufw allow from 192.168.1.0/24 to any port 5432 firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.1.0/24 port port=5432 protocol=tcp accept' firewall-cmd --reload
3. 数据库常见问题排查 3.1 MySQL常见问题 连接问题 1 2 3 4 5 6 7 8 9 10 11 systemctl status mysql netstat -tuln | grep 3306 iptables -L -n | grep 3306 tail -f /var/log/mysql/error.log
性能问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 SHOW PROCESSLIST;SHOW TABLE STATUS FROM database_name;SHOW GLOBAL STATUS LIKE 'Com_%' ;SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_%' ;SHOW GLOBAL STATUS LIKE 'Threads_%' ;SHOW GLOBAL STATUS LIKE 'Qcache_%' ; SHOW INDEX FROM table_name;
磁盘空间问题 1 2 3 4 5 6 7 8 du -sh /var/lib/mysql/du -sh /var/lib/mysql/database_name/SELECT table_schema, ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" FROM information_schema.tables GROUP BY table_schema;
3.2 PostgreSQL常见问题 连接问题 1 2 3 4 5 6 7 8 9 10 11 systemctl status postgresql netstat -tuln | grep 5432 grep -v "^#" /etc/postgresql/13/main/pg_hba.conf | grep -v "^$" tail -f /var/log/postgresql/postgresql-13-main.log
性能问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 SELECT pid, usename, datname, state, query, now() - query_start AS durationFROM pg_stat_activity WHERE state = 'active' ;SELECT pid, now() - query_start AS duration, query FROM pg_stat_activity WHERE state = 'active' AND now() - query_start > '1 minute' ::interval ;SELECT schemaname, relname, seq_scan, seq_tup_read, idx_scan, idx_tup_fetchFROM pg_stat_user_tables;SELECT schemaname, relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetchFROM pg_stat_user_indexes;
磁盘空间问题 1 2 3 4 5 6 7 8 9 10 11 12 du -sh /var/lib/postgresql/13/main/SELECT pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size FROM pg_database; SELECT nspname || '.' || relname AS "relation" , pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size" FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname NOT IN ('pg_catalog' , 'information_schema' ) AND C.relkind <> 'i' AND nspname !~ '^pg_toast' ORDER BY pg_total_relation_size(C.oid) DESC LIMIT 20;
4. 高可用和水平扩展 4.1 MySQL主从复制 1 2 3 4 5 6 7 8 9 server-id = 1 log_bin = mysql-binbinlog_format = ROWbinlog_do_db = database_name server-id = 2 relay_log = mysql-relay-bin
在主服务器上创建复制用户:
1 2 3 4 5 6 CREATE USER 'repl' @'%' IDENTIFIED BY 'password' ;GRANT REPLICATION SLAVE ON * .* TO 'repl' @'%' ;FLUSH PRIVILEGES; SHOW MASTER STATUS;
在从服务器上配置复制:
1 2 3 4 5 6 7 8 9 10 11 12 CHANGE MASTER TO MASTER_HOST= 'master_ip' , MASTER_USER= 'repl' , MASTER_PASSWORD= 'password' , MASTER_LOG_FILE= 'mysql-bin.000001' , MASTER_LOG_POS= 1234 ; START SLAVE;SHOW SLAVE STATUS\G
4.2 PostgreSQL主从复制 在主服务器上配置 (postgresql.conf):
1 2 3 4 5 listen_addresses = '*' wal_level = replicamax_wal_senders = 10 max_replication_slots = 10 wal_keep_size = 64
在主服务器上编辑 pg_hba.conf:
1 host replication replica replica_ip/32 md5
创建复制用户:
1 CREATE ROLE replica WITH REPLICATION LOGIN PASSWORD 'password' ;
在从服务器上进行基础备份:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 systemctl stop postgresql rm -rf /var/lib/postgresql/13/main/*pg_basebackup -h master_ip -U replica -D /var/lib/postgresql/13/main -P -v -X stream touch /var/lib/postgresql/13/main/standby.signalecho "primary_conninfo = 'host=master_ip port=5432 user=replica password=password'" >> /var/lib/postgresql/13/main/postgresql.confecho "recovery_target_timeline = 'latest'" >> /var/lib/postgresql/13/main/postgresql.confchown -R postgres:postgres /var/lib/postgresql/13/mainchmod 700 /var/lib/postgresql/13/mainsystemctl start postgresql
5. 数据库性能调优最佳实践 5.1 硬件配置建议
存储 :
数据库文件使用SSD存储
单独磁盘挂载点用于数据、日志和备份
RAID 10配置平衡性能和冗余
内存 :
足够大的内存以容纳活跃数据集
MySQL: 服务器内存的70-80%分配给InnoDB缓冲池
PostgreSQL: 服务器内存的25%分配给共享缓冲区,75%留给文件系统缓存
CPU :
多核CPU以支持并行查询和并发连接
较高的单核性能有助于提高查询性能
5.2 索引优化
有效索引 :
为频繁出现在WHERE、JOIN、ORDER BY和GROUP BY子句中的列创建索引
将选择性最高的列放在复合索引的最前面
避免创建重叠的索引
定期检查和删除未使用的索引
索引类型选择 :
MySQL: 考虑使用哈希索引、全文索引或空间索引
PostgreSQL: 考虑B-tree、Hash、GiST、SP-GiST、GIN或BRIN索引
5.3 查询优化
查询重写 :
只选择必要的列(避免SELECT *
)
使用精确的WHERE条件减少扫描的数据量
避免使用函数应用在索引列上
考虑使用LIMIT限制结果集大小
JOIN优化 :
选择正确的JOIN类型(INNER、LEFT、RIGHT)
连接条件应该使用索引
按从小到大的顺序连接表
避免不必要的子查询,使用JOIN代替
5.4 连接池和应用侧优化
使用连接池 :
例如: PgBouncer (PostgreSQL)、ProxySQL (MySQL)
减少频繁创建和销毁连接的开销
合理设置最小和最大连接数
批处理操作 :
使用批量INSERT而不是多个单行INSERT
对于大量数据加载,考虑使用LOAD DATA或COPY命令
事务优化:批量提交多个更改
总结 本文详细介绍了MySQL和PostgreSQL两大主流开源数据库的安装配置、性能优化、备份恢复和安全加固。数据库作为应用程序的核心组件,其性能和可靠性对整个系统的影响至关重要。通过合理的配置优化、定期的维护管理和适当的监控机制,可以确保数据库系统稳定、高效地运行。
无论是选择MySQL还是PostgreSQL,都应该根据具体的应用需求和业务特点,选择合适的配置方案和优化策略。同时,保持良好的备份习惯和安全实践,可以有效防范数据丢失和安全风险。