from urllib.request import urlopen
import re
import time
import datetime
import random
import linecache
import openpyxl
import pymysql
import re
import requests
import os
def mkdir(path):
import os # 用于创建文件夹
# 去除首位空格
path = path.strip()
# 去除尾部 \ 符号
path = path.rstrip("\\")
# 判断路径是否存在
# 存在 true
# 不存在 false
isExits = os.path.exists(path)
# 判断结果
if not isExits:
os.makedirs(path) # 不存在则创建该目录
print(path + " 创建成功")
return True
else:
print(path + " 目录已经存在")
return False
########################################
#登录mysql并做成游标
try:
#登录数据库
db = pymysql.connect(
host="localhost",
user="root",
password="123qwe"
)
print("连接成功:")
except Exception as e:
print("连接失败:", e)
con = db.cursor()
#####################################################################
#定义变量
data_time = datetime.datetime.now().strftime('%Y-%m-%d')
data_time1 = datetime.datetime.now().strftime('%m-%d')
#####################################################################
#创建备份文件夹
mkdir(f"D:\\backup\\{data_time}_buckup")
###################################################################
sql = "show databases;"
con.execute(sql)
database = [con.fetchall()]
database_list = re.findall('(\'.*?\')',str(database))
database_list = [re.sub("'",'',each) for each in database_list]
#print(database_list)
for i in database_list:
file_name_table = i
#print(i)
#mkdir(f"D:\\backup\\{data_time}_buckup\\{i}_buckup")
if i := re.match(r'\d{6}_data',i):
mkdir(f"D:\\backup\\{data_time}_buckup\\{file_name_table}_buckup")
database_name = (i.group())
sql = f"use {database_name};"
con.execute(sql)
sql = f"show tables;"
con.execute(sql)
table = [con.fetchall()]
table_list = re.findall('(\'.*?\')',str(table))
table_list = [re.sub("'",'',each) for each in table_list]
print(table_list)
bakup_dir = r'D:\mysql-5.7.31-winx64\bin'
usr = root
password = 123qwe
os.chdir(bakup_dir)
for i in table_list:
print(i)
file_backup=(f"D:\\backup\\{data_time}_buckup\\{file_name_table}_buckup\\{data_time1}_{i}_buckup")
#print(file_backup)
mysqlbak_cmd = f"mysqldump -u{usr} -p{password} --source-data=2 -F -E -R --triggers --single-transaction --flush-privileges --default-character-set=utf8 --hex-blob -B {database_name} {i}> {file_backup}"
print(mysqlbak_cmd)
os.system(mysqlbak_cmd)
print("backup sucessful")
#退出数据库
db.close()
#!/bin/bash
#
#****************************************************
#Author: caojidong
#QQ: 1549396190
#Date: 2022-01-23
#FileName: buckup.sh
#cell-phone number: 13739548267
#Description: test
#Copyright(C): 2023 All right
#***************************************************
#!/bin/bash
TIME=`date +%F_%H-%M-%S`
DIR=/backup
PASS=123456
USER=root
[ -d "$DIR" ] || mkdir $DIR
data_list=`mysql -uroot -e 'show databases;'|grep -vE 'mysql|.*schema|sys'`
for i in ${data_list};do mysqldump -u${USER} --source-data=2 -F -E -R --triggers --single-transaction --flush-privileges --default-character-set=utf8 --hex-blob -B ${i} |gzip > ${DIR}/${i}_`date +%F`.sql.gz
done
#!/bin/bash
#
#****************************************************
#Author: caojidong
#QQ: 1549396190
#Date: 2022-1-26
#FileName: 1.test.sh
#cell-phone number: 13739548267
#Description: test
#Copyright(C): 2022 All right
#***************************************************
dir_root="/root/"
dir_file_name=`ls ${dir_root} |grep *buckup`
echo ${dir_file_name}
#cao=`echo -e "\e[40;37m黑底白字 \e[0m"`
func_judge_YN(){
cao=`echo -e "\e[40;37m${1} \e[0m"`
read -ep "${cao}(y/n)" INPUT
INPUT=`echo $INPUT | tr 'A-Z' 'a-z'`
case $INPUT in
y|yes)
echo "You input is YES"
;;
n|no)
#回答N退出循环
echo "You input is NO"; exit
;;
*)
echo "Input fales,please input yes or no!"
exit
;;
esac
}
func_judge_YN "是否还原 ${dir_file_name} 的数据"
cd /root/${dir_file_name}
list_data=`ls`
#echo "${list_data}"
for i in ${list_data};do
#echo "$i"
cd /root/${dir_file_name}/${i}
list_table=`ls`
for i in ${list_table};do
echo ${i}
mysql < ${i}
done
done
一、修改密码方法
1、mysqladmin -uroot -p'旧密码' password‘新密码’(在liunx界面输入)
2、mysql>alter user user() identified by '密码';
3、mysql>update mysql.user set authentication_string=password('密码') where user='root';
二、忘记密码
ps -ef | grep -i mysql #查看mysql服务状态
systemctl stop mysqld #关闭防火墙
vi /etc/my.cnf 也可能在/etc/mysql/my.cnf
#修改配置文件
添加 在【mysqld】下添加 skip-grant-tables
#作用是登录mysql的时候跳过密码验证
然后启动mysql服务,并进入mysql
mysql> mysql -uroot -p #无密码进入
mysql> use mysql;
mysql> update mysql.user set authentication_string=password('root_password') where user='root';
mysql> flush privileges;
mysql> \q
systemctl start mysqld
(以上借鉴https://www.cnblogs.com/wuotto/p/9682400.html)
三、修改密码策略(密码等级)
我们登陆时候密码等级设定为最高级;如何改变呢?
密码等级的设置在 mysql variables 中
可以先打开variables 看看
show variables ;
可以看到变量很多;
用show variables like '%搜索内容%'
mysql >show variables like '%pass%';#找到修改密码等级的变量信息。
如果出现报错:ERROR 1682 (HY000): Native table 'performance_schema'.'session_variables' has the wrong structure
----从mysql5.7.6开始information_schema.global_status已经开始被舍弃,为了兼容性,此时需要打开 show_compatibility_56
mysql>set @@global.show_compatibility_56=ON; #打开show_compatibility_56
关于 mysql 密码策略相关参数;
1)、validate_password_length 固定密码的总长度;
2)、validate_password_dictionary_file 指定密码验证的文件路径;
3)、validate_password_mixed_case_count 整个密码中至少要包含大/小写字母的总个数;
4)、validate_password_number_count 整个密码中至少要包含阿拉伯数字的个数;
5)、validate_password_policy 指定密码的强度验证等级,默认为 MEDIUM;(medium)
设置密码密级。
mysql >set global validate_password_policy=LOW; #首先需要设置密码的验证强度等级,设置 validate_password_policy 的全局参数为 LOW 即可
mysql > set global validate_password_length=6; #当前密码长度为 8 ,如果不介意的话就不用修改了,按照通用的来讲,设置为 6 位的密码,设置 validate_password_length 的全局参数为 6 即可,
mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';#修改密码为123456