当前位置:首页 > 问答 > 正文

数据库运维 系统管理 CentOS7 MariaDB 管理指南

📊 数据库运维 | 系统管理 | CentOS7 MariaDB 管理指南

场景引入
凌晨3点,你的手机突然狂震——生产环境的订单系统挂了!📱💥 登录服务器一看,MariaDB服务悄无声息地罢工了...别慌!这份CentOS7下的MariaDB生存指南,就是你的深夜救星✨


🔧 基础安装与配置

安装MariaDB

# 添加官方仓库(CentOS7默认仓库版本较旧)  
sudo tee /etc/yum.repos.d/MariaDB.repo <<EOF  
[mariadb]  
name = MariaDB  
baseurl = https://mirrors.aliyun.com/mariadb/yum/10.5/centos7-amd64/  
gpgkey = https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB  
gpgcheck = 1  
EOF  
# 安装服务端和客户端  
sudo yum install -y MariaDB-server MariaDB-client  
# 启动并设置开机自启  
sudo systemctl start mariadb  
sudo systemctl enable mariadb  

💡 小贴士:如果遇到GPG密钥报错,试试 sudo rpm --import https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB

安全初始化

sudo mysql_secure_installation  

跟着提示走:设置root密码🚪、移除匿名用户👤、禁止远程root登录🚫...(建议全部选Y)

数据库运维 系统管理 CentOS7 MariaDB 管理指南


🛠️ 日常运维操作

启停服务

# 优雅重启(推荐)  
sudo systemctl restart mariadb  
# 强制终止(慎用❌)  
sudo kill -9 $(pgrep mysqld)  

备份与恢复

逻辑备份

# 全库备份(含数据+结构)  
mysqldump -u root -p --all-databases > full_backup_$(date +%F).sql  
# 单库备份  
mysqldump -u root -p orders_db > orders_backup.sql  

物理备份(更快速):

sudo systemctl stop mariadb  
sudo rsync -av /var/lib/mysql/ /backup/mysql/  
sudo systemctl start mariadb  

🚨 故障排查三板斧

查看错误日志

sudo tail -100 /var/log/mariadb/mariadb.log  
# 常见关键词:ERROR、crash、OOM(内存不足)💥  

连接数爆炸怎么办?

-- 查看当前连接  
SHOW STATUS LIKE 'Threads_connected';  
-- 终止卡死连接(替换ID)  
KILL 114514;  

性能优化速查

# /etc/my.cnf 关键参数调整  
[mysqld]  
innodb_buffer_pool_size = 4G    # 建议分配物理内存的50-70%  
max_connections = 500           # 根据业务需求调整  
slow_query_log = 1              # 开启慢查询记录🐌  

🔐 安全加固清单

  1. 定期改密码

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('新密码');  
  2. 权限最小化

    数据库运维 系统管理 CentOS7 MariaDB 管理指南

    -- 示例:给应用账号仅赋单库读写权  
    GRANT SELECT,INSERT ON shop_db.* TO 'app_user'@'192.168.1.%' IDENTIFIED BY '强密码';  
  3. 防火墙拦截

    sudo firewall-cmd --permanent --add-service=mysql  
    sudo firewall-cmd --reload  

💡 高级技巧

跳过密码验证(忘记root密码时)

sudo systemctl stop mariadb  
sudo mysqld_safe --skip-grant-tables &  
mysql -u root  
# 进入后执行:FLUSH PRIVILEGES; 再改密码  

监控表空间使用

SELECT table_schema "数据库",  
ROUND(SUM(data_length+index_length)/1024/1024,2) "大小(MB)"  
FROM information_schema.tables  
GROUP BY table_schema;  

最后叮嘱

  • 任何操作前先备份!💾
  • 批量删除数据前先用SELECT验证!⚠️
  • 遇到诡异问题先查日志再谷歌(2025年了可能叫"星辰搜索"?🌌)

现在你可以淡定地端起咖啡☕,对老板说:"数据库已恢复,正在分析根本原因..." 😎

发表评论