来自公众号:入门小站
在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=7find $BACKUP_DIR -type f -mtime +$DAYS_TO_KEEP -exec rm {} \;
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"mysqladmin -u $MYSQL_USER -p$MYSQL_PASSWORD processlist
#!/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
#!/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;"
#!/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 等)为适当的值。