1. 数据库管理概述
数据库管理是IT系统运维的重要组成部分,包括数据库安装配置、日常维护、备份恢复、性能优化、故障排查等。有效的数据库管理可以确保数据的安全性和可靠性,提高系统的性能和稳定性。更多学习教程www.fgedu.net.cn
2. 数据库安装与配置
数据库安装与配置是数据库管理的基础,需要根据实际需求选择合适的数据库版本和配置参数。
# yum install -y mysql-server
# 启动MySQL服务
# systemctl start mysqld
# systemctl enable mysqld
# 查看MySQL服务状态
# systemctl status mysqld
● mysqld.service – MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2026-03-30 10:00:00 CST; 1h ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 12345 ExecStart=/usr/sbin/mysqld –daemonize –pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Main PID: 12346 (mysqld)
CGroup: /system.slice/mysqld.service
└─12346 /usr/sbin/mysqld –daemonize –pid-file=/var/run/mysqld/mysqld.pid
# 配置MySQL
# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
Please set the password for root here.
New password:
Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No): y
Success.
Normally, root should only be allowed to connect from
‘fgedudb’. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No): y
Success.
By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No): y
– Dropping test database…
Success.
– Removing privileges on test database…
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No): y
Success.
All done!
3. 数据库日常维护
数据库日常维护包括数据库状态检查、表优化、日志管理等,确保数据库的正常运行。
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123
Server version: 8.0.28 MySQL Community Server – GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
# 查看数据库状态
mysql> SHOW STATUS;
+————————–+————+
| Variable_name | Value |
+————————–+————+
| Aborted_clients | 0 |
| Aborted_connects | 0 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Bytes_received | 152 |
| Bytes_sent | 1440 |
| Com_admin_commands | 0 |
| Com_begin | 0 |
| Com_commit | 0 |
| Com_delete | 0 |
| Com_insert | 0 |
| Com_select | 1 |
| Com_update | 0 |
| Connections | 1 |
| Created_tmp_disk_tables | 0 |
| Created_tmp_tables | 0 |
| Innodb_buffer_pool_pages_data | 21 |
| Innodb_buffer_pool_pages_free | 1006 |
| Innodb_buffer_pool_pages_total | 1027 |
| Innodb_buffer_pool_size | 16777216 |
+————————–+————+
# 优化表
mysql> OPTIMIZE TABLE users;
+———-+———-+———-+—————————–+
| Table | Op | Msg_type | Msg_text |
+———-+———-+———-+—————————–+
| test.users | optimize | status | OK |
+———-+———-+———-+—————————–+
# 查看数据库列表
mysql> SHOW DATABASES;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+——————–+
4. 数据库备份与恢复
数据库备份与恢复是数据库管理的重要环节,需要定期备份数据库,确保数据的安全性和可恢复性。
# mysqldump -u root -p test > test_backup.sql
Enter password:
# 查看备份文件大小
# ls -lh test_backup.sql
-rw-r–r– 1 root root 1.2K Mar 30 10:00 test_backup.sql
# 恢复数据库
# mysql -u root -p test < test_backup.sql
Enter password:
# 使用xtrabackup备份数据库
# xtrabackup --backup --target-dir=/backup/mysql
xtrabackup: recognized server arguments: --datadir=/var/lib/mysql
xtrabackup: recognized client arguments: --backup=1 --target-dir=/backup/mysql
xtrabackup version 8.0.28 based on MySQL server 8.0.28 Linux (x86_64) (revision id: 12345)
xtrabackup: cd to /var/lib/mysql
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: using O_DIRECT
xtrabackup: creating backup directory /backup/mysql
xtrabackup: backup directory /backup/mysql successfully created
xtrabackup: starting threads to backup non-InnoDB tables and files
xtrabackup: backuptables
xtrabackup: Backing up files '/var/lib/mysql/*.frm' to '/backup/mysql'
xtrabackup: Backing up files '/var/lib/mysql/*.ibd' to '/backup/mysql'
xtrabackup: Backing up file '/var/lib/mysql/ibtmp1' to '/backup/mysql/ibtmp1'
xtrabackup: Backing up files '/var/lib/mysql/*.opt' to '/backup/mysql'
xtrabackup: Backing up files '/var/lib/mysql/*.cnf' to '/backup/mysql'
xtrabackup: Backing up file '/var/lib/mysql/ib_buffer_pool' to '/backup/mysql/ib_buffer_pool'
xtrabackup: Backing up files '/var/lib/mysql/*.pid' to '/backup/mysql'
xtrabackup: Backing up files '/var/lib/mysql/*.sock' to '/backup/mysql'
xtrabackup: Finished backing up non-InnoDB tables and files
xtrabackup: Starting to backup InnoDB tables and files
xtrabackup: Backing up InnoDB tablespace 'innodb_system'
xtrabackup: Backing up InnoDB tablespace 'innodb_undo_001'
xtrabackup: Backing up InnoDB tablespace 'innodb_undo_002'
xtrabackup: Backing up InnoDB tablespace 'test/users'
xtrabackup: Backing up InnoDB tablespace 'test/orders'
xtrabackup: Backing up InnoDB log files
xtrabackup: Backing up file './ib_logfile0'
xtrabackup: Backing up file './ib_logfile1'
xtrabackup: Finished backing up InnoDB tables and files
xtrabackup: Transaction log of lsn (12345) to (67890) was backed up
xtrabackup: MySQL binlog position: filename 'binlog.000001', position '12345'
xtrabackup: Finished backup at 2026-03-30 10:00:00
xtrabackup: Writing backup-my.cnf
xtrabackup: Redo log (from LSN 12345 to 67890) was generated.
xtrabackup: This log was created and must be applied in order for the backup to be consistent.
xtrabackup: So, please set innodb_log_file_size to at least 50331648 in my.cnf
xtrabackup: and copy the generated ib_logfile0 and ib_logfile1 to MySQL datadir.
xtrabackup: Log copied to '/backup/mysql/ib_logfile0'
xtrabackup: Log copied to '/backup/mysql/ib_logfile1'
xtrabackup: This backup was taken with the --backup option.
xtrabackup: The xtrabackup binary can be used to restore it.
xtrabackup: The directory '/backup/mysql' is now ready for use as a backup.
5. 数据库性能优化
数据库性能优化包括查询优化、索引优化、配置优化等,提高数据库的运行速度和响应时间。
# vi /etc/my.cnf
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 1
# 重启MySQL服务
# systemctl restart mysqld
# 查看慢查询日志
# tail -f /var/log/mysql/mysql-slow.log
# Time: 2026-03-30T10:00:00.000000Z
# User@Host: root[root] @ fgedudb [] Id: 1
# Query_time: 1.234567 Lock_time: 0.000000 Rows_sent: 1000 Rows_examined: 1000000
SET timestamp=1234567890;
SELECT * FROM users WHERE age > 30;
# 优化查询
mysql> EXPLAIN SELECT * FROM users WHERE age > 30;
+—-+————-+——-+————+——+—————+——+———+——+———+———-+————-+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+—-+————-+——-+————+——+—————+——+———+——+———+———-+————-+
| 1 | SIMPLE | users | NULL | ALL | NULL | NULL | NULL | NULL | 1000000 | 33.33 | Using where |
+—-+————-+——-+————+——+—————+——+———+——+———+———-+————-+
# 创建索引
mysql> CREATE INDEX idx_age ON users(age);
Query OK, 0 rows affected (1.23 sec)
Records: 0 Duplicates: 0 Warnings: 0
# 再次执行查询
mysql> EXPLAIN SELECT * FROM users WHERE age > 30;
+—-+————-+——-+————+——-+—————+———+———+——+——–+———-+————-+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+—-+————-+——-+————+——-+—————+———+———+——+——–+———-+————-+
| 1 | SIMPLE | users | NULL | range | idx_age | idx_age | 4 | NULL | 333333 | 100.00 | Using where |
+—-+————-+——-+————+——-+—————+———+———+——+——–+———-+————-+
# 优化MySQL配置
# vi /etc/my.cnf
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
max_connections = 1000
query_cache_size = 0
query_cache_type = 0
6. 数据库故障排查
数据库故障排查是数据库管理的重要技能,需要掌握各种数据库诊断工具和方法,快速定位和解决数据库问题。
# tail -f /var/log/mysqld.log
2026-03-30T10:00:00.000000Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1, error: 11
2026-03-30T10:00:00.000000Z 0 [ERROR] InnoDB: Unable to open the first data file
2026-03-30T10:00:00.000000Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2026-03-30T10:00:00.000000Z 0 [ERROR] MySQL Server – Different lower_case_table_names settings for server (‘1’) and data dictionary (‘0’).
2026-03-30T10:00:00.000000Z 0 [ERROR] Aborting
# 检查数据库连接
# mysqladmin -u root -p ping
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysql is alive
# 检查数据库进程
# ps aux | grep mysql
mysql 12346 0.5 1.0 1234567 102400 ? Sl 10:00 0:01 /usr/sbin/mysqld –daemonize –pid-file=/var/run/mysqld/mysqld.pid
root 12347 0.0 0.0 123456 1234 pts/0 S+ 10:01 0:00 grep –color=auto mysql
# 检查数据库端口
# netstat -tulpn | grep mysql
tcp6 0 0 :::3306 :::* LISTEN 12346/mysqld
7. 数据库安全管理
数据库安全管理包括用户权限管理、密码策略、访问控制等,确保数据库的安全性和可靠性。
mysql> CREATE USER ‘user1’@’fgedudb’ IDENTIFIED BY ‘password123’;
Query OK, 0 rows affected (0.01 sec)
# 授予用户权限
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON test.* TO ‘user1’@’fgedudb’;
Query OK, 0 rows affected (0.01 sec)
# 查看用户权限
mysql> SHOW GRANTS FOR ‘user1’@’fgedudb’;
+————————————————-+
| Grants for user1@fgedudb |
+————————————————-+
| GRANT USAGE ON *.* TO `user1`@`fgedudb` |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO `user1`@`fgedudb` |
+————————————————-+
# 修改用户密码
mysql> ALTER USER ‘user1’@’fgedudb’ IDENTIFIED BY ‘newpassword123’;
Query OK, 0 rows affected (0.01 sec)
# 删除用户
mysql> DROP USER ‘user1’@’fgedudb’;
Query OK, 0 rows affected (0.01 sec)
# 配置密码策略
# vi /etc/my.cnf
[mysqld]
validate_password_policy = STRONG
validate_password_length = 8
validate_password_mixed_case_count = 1
validate_password_number_count = 1
validate_password_special_char_count = 1
8. 数据库管理最佳实践
数据库管理最佳实践包括定期备份、监控数据库性能、优化数据库配置、制定安全策略等。
- 定期备份数据库,确保数据的安全性和可恢复性
- 监控数据库性能,及时发现并解决性能问题
- 优化数据库查询和索引,提高数据库性能
- 配置合理的数据库参数,根据实际需求调整
- 实施数据库安全措施,保护数据的安全性
- 定期进行数据库维护,包括表优化、日志清理等
- 建立数据库文档管理系统,记录数据库结构和配置
- 培训数据库管理人员,提高数据库管理技能
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
