嵌入式技术
在Linux中,使用MySQL进行常见的运维任务时,可以编写一些脚本来简化操作。以下是一些常用的MySQL运维脚本,希望对你的工作有所帮助.
备份数据库:
#!/bin/bash BACKUP_DIR="/path/to/backup/directory" MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" DATABASE="your_database_name" DATE=$(date +%Y%m%d%H%M%S) mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE > $BACKUP_DIR/$DATABASE-$DATE.sql
优化和修复数据库表:
#!/bin/bash MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" DATABASE="your_database_name" mysqlcheck -u $MYSQL_USER -p$MYSQL_PASSWORD --auto-repair --optimize $DATABASE
清除过期的备份文件:
#!/bin/bash BACKUP_DIR="/path/to/backup/directory" DAYS_TO_KEEP=7 find $BACKUP_DIR -type f -mtime +$DAYS_TO_KEEP -exec rm {} ;
监控MySQL进程状态:
#!/bin/bash MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" mysqladmin -u $MYSQL_USER -p$MYSQL_PASSWORD processlist
监控MySQL运行状态信息:
#!/bin/bash MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" mysqladmin -u $MYSQL_USER -p$MYSQL_PASSWORD extended-status
查看数据库大小:
#!/bin/bash MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" DATABASE="your_database_name" mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "SELECT table_schema AS 'Database Name', SUM(data_length + index_length) / 1024 / 1024 AS 'Size (MB)' FROM information_schema.TABLES GROUP BY table_schema;"
导出数据库表结构:
#!/bin/bash MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" DATABASE="your_database_name" mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD --no-data $DATABASE > schema.sql
导入数据库表结构:
#!/bin/bash MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" DATABASE="your_database_name" mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE < schema.sql
锁定MySQL数据库:
#!/bin/bash MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "FLUSH TABLES WITH READ LOCK;"
解锁MySQL数据库:
#!/bin/bash MYSQL_USER="your_mysql_user" MYSQL_PASSWORD="your_mysql_password" mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "UNLOCK TABLES;"
请注意:在使用这些脚本之前,你需要替换占位符(your_mysql_user,your_mysql_password,your_database_name,/path/to/backup/directory 等)为适当的值。
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !