show variables like '%max_connect_errors%'
ip连接mysql连接错误一次,然后mysql是给记录一次,当达到配置文件中允许出错的最大次数时,这个ip就连接不上了
show variables like '%max_connections%'
show global status like 'Max_used_connections'
flush hosts;
1、插入语句(加条件)
mysql插入语句是不能直接添加where条件的,可以新建一个临时表进行操作
DUAL :临时表
sql = """insert into poc_result(task_id,poc_name,cve,target,ip) SELECT '%s','%s','%s','%s','%s' FROM DUAL WHERE NOT EXISTS(SELECT task_id FROM poc_result WHERE task_id = '%s' and poc_name='%s')"""%(task_id,poc_name,cve,target_url,ip,ip,poc_name)
2、将查询出的字段类型改为字符串类型
使用CONCAT(字段,")
select if(`name` is NULL,'http',`name`) as `name`,host,concat(port,'') as port from server_result
3、分组查询将结果相加
将字段a相加并用分割符连接
GROUP_CONCAT(字段a separator ‘分割符’)
select `host`,GROUP_CONCAT(`port` separator ';') as `port`,`name` from server_result GROUP BY `host`
4、查询某个表字段
SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'tablename'
5、存在更新不存在插入语句
INSERT INTO tb_addrbook(num,name,mobile) VALUE('1001','小李','13112345678') ON DUPLICATE KEY UPDATE name= '小李',mobile='13112345678'
6、将某个字段值更新为原来的值+1
update 表 set 字段 = 字段 + 1 where 条件
7、查询所有表名
select table_name from information_schema.tables where table_schema='数据库表名'
8、删除表
drop table if exists 表名
1、安装包
pip3 install DBUtils==1.3
pip3 install PyMySQL==0.10.1
2、代码
import pymysql
from DBUtils.PooledDB import PooledDB, SharedDBConnection
class SqlHelper:
def __init__(self):
self.pool = PooledDB(
creator=pymysql, # 使用数据库连接的模块
maxconnections=6, # 连接池允许连接的最大连接数,0和None表无限制
mincached=2, # 初始化时,连接池中至少创建的空闲连接
maxcached=5, # 连接池中闲置的最大连接数,0和None表示无限制
maxshared=3, # 连接池中最多共享的连接数量,0和None博爱是无限制
blocking=True, # 连接池中无可用链接后,是否阻塞等待
maxusage=None, # 一个连接最多被重复使用的次数,0和None表示无限制
setsession=[], # 开始会话前执行的命令列表
ping=0, # ping mysql服务器,0和None表示从不,1 ping1次
host='172.16.0.25',
port=3306,
user='root',
password='123456',
database='scan_db_clone',
charset='utf8mb4'
)
def conn_db(self):
try:
conn = self.pool.connection()
return conn
except:
print('conn is error')
def close(self, cursor, conn):
try:
cursor.close()
conn.close()
except:
print('close is error')
def select_sql(self, sql):
conn = self.conn_db()
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql)
result = cursor.fetchall()
return result
except Exception as e:
print(e)
finally:
self.close(cursor, conn)
def insert_sql(self, sql):
conn = self.conn_db()
cursor = conn.cursor()
try:
cursor.execute(sql)
conn.commit()
return True
except Exception as e:
print(e)
conn.rollback()
finally:
self.close(cursor, conn)
def update_sql(self, sql):
conn = self.conn_db()
cursor = conn.cursor()
try:
cursor.execute(sql)
conn.commit()
return True
except Exception as e:
print(e)
conn.callback()
finally:
self.close(cursor, conn)
def delete_sql(self, sql):
conn = self.conn_db()
cursor = conn.cursor()
try:
cursor.execute(sql)
conn.commit()
return True
except Exception as e:
print(e)
conn.rollback()
finally:
self.close(cursor, conn)
1、登录mysql
mysql -uroot -p
2、切换数据库,设置,刷新缓存,退出
mysql>use mysql;
mysql>update user set host = '%' where user ='root';
mysql>flush privileges;
mysql>quit