MySQL实际使用及遇到的问题

常见操作

从本地文件导入

mysql -h 10.26.9.29 -u root -P 6377 -pspiderMysql -D ai_narrator -e "load data local infile 'config/city_params_201905272122.txt' into table city_params fields terminated by '\t'"

从本地文件导入-自增主键

删表

DROP TABLE mysql_tb # 但是发现实在是太慢了

清除数据

DELETE FROM mysql_tb # 可以返回被删除的记录数
TRUNCATE TABLE mysql_tb # 返回的是0

重设最大链接数量

如果出现最大连接造成的问题,可以先登录MySQL,然后重设最大连接数量

set global max_connections=1000 

注意事项(python版)

  1. 增加重连机制
import pymysql.cursor

# 建立连接
class MySQLPool(object):
    def __init__(self, host, port, pwd, db, user, charset):
        self.conn = pymysql.connect(host=host
                        , port=port
                        , password=pwd
                        , db=db
                        , user=user
                        , charset=charset)
                        
    def get_cursor(self):
        """增加重连机制"""
        self.conn.ping(reconnect=True)
        cursor = self.conn.cursor()
        return cursor
  1. 增加容错机制
# 读取数据                        
with self.mysql_pool.get_cursor() as cursor:
    sql = "SELECT {} FROM ai_narrator.ai_narrator_city_params WHEcity_code= '{}'"\
            .format(','.join(self.CITY_PARAM_LST), city_code)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        self.city_result = result
        # 注意解析,和存入时保持一致就行。
    except Exception as e:
        pass

MySQL知识

发布了120 篇原创文章 · 获赞 35 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/u012328476/article/details/102872567