Category - MySQL相关

2024-06-19 16:04:41    137    0    0
  1. #MySQL备份脚本
  2. #!/bin/sh
  3. #定义数据库host及备份目录
  4. backup_host="10.0.0.1"
  5. backup_dir=/data/mysql_bak
  6. # 定义数据库账号密码及备份语句
  7. mysql_user="user"
  8. mysql_password="passwd"
  9. mysqldump_opts="-h $backup_host -u $mysql_user -p$mysql_password --default-character-set=utf8"
  10. date=$(date '+%Y%m%d%H%M')
  11. #以下为定义数据库库名,二选一
  12. # 定义数据库库名
  13. databases='databasename1 databasename2'
  14. # 自动获取数据库库名
  15. databases=$(mysql -h $backup_host -u $mysql_user -p$mysql_password -e 'SHOW DATABASES;' | grep -Ev "(Database|information_schema|performance_schema|mysql|sys)")
  16. #测试数据库
  17. umask 0077
  18. test ! -d "$backup_dir" && mkdir -p "$backup_dir"
  19. test ! -w "$backup_dir" && echo "Error: $backup_dir is un-writeable." && exit 0
  20. # 循环备份
  21. for database in $databases
  22. do
  23. /usr/local/mysql/bin/mysqldump $mysqldump_opts $database > "$backup_dir/$database-$date.sql"
  24. done
  25. # 打包备份文件并删除7天前的备份
  26. cd "$backup_dir"
  27. tar zcvf "mysqlbak_$date.tar.gz" *.sql
  28. rm -rf *.sql
  29. find "$backup_dir" -type f -mtime +7
2024-06-19 15:25:42    39    0    0

CentOS7安装

  1. rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
  2. wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
  3. yum install mysql-community-server
  4. systemctl start mysqld
  5. systemctl enable mysqld
  6. grep "A temporary password" /var/log/mysqld.log
  7. mysql_secure_installation

其他常用命令

  1. #清空某张表的数据
  2. TRUNCATE TABLE tablename;
  3. #给某用户某个数据库的权限
  4. grant all on dbname.* to 'username'@'%';
  5. flush privileges;
  6. #给某个用户所有数据库权限
  7. GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;
Footer