docker pull mysql:5.7

docker run -itd -p 8070:3306 -v /hadoop-data/work/sl/project/mysql/data:/var/lib/mysql -v /hadoop-data/work/sl/project/mysql/conf.d:/etc/mysql/conf.d --name=qa_mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

mysql -h host_ip -P port -u root -p123456

show databases;

show tables from database_name;         # use database_name; show tables;

show columns from table_name;           # desc table_name;

seletc * from table_name limit n;       # head n row data

select * from table_name where id = 0;  # conditional search

select count(id) from table_name;       # data total

delete from table_name;                 # delete table data

1. pymysql.err.DataError: (1366, “Incorrect string value: ‘\xE5\xA4\xA9\xE6\xB9\x96…’ for column ‘question’ at row 1”)

原因:由于建表的时候没有指定数据库字符集, 保存中文的时候就会报错:pymysql.err.InternalError: (1366, …)

mysql>ALTER TABLE your_table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
or
mysql>alter table 表名 convert to character set utf8mb4;

2.(2006, “MySQL server has gone away (BrokenPipeError(32, ‘Broken pipe’))”);

默认超8小时,连接自动断开

    def _is_alive(self):
        """检查连接是否失活"""
        try:
            self.conn.ping(reconnect=True)
        except:
            self.conn = pymysql.connect(host=MYSQL_HOST,
                                        user=MYSQL_USER,
                                        port=MYSQL_PORT,
                                        password=MYSQL_PWD,
                                        database=MYSQL_DB,
                                        charset='utf8mb4',
                                        local_infile=True)
            self.cursor = self.conn.cursor()

Share on: TwitterFacebookEmail

Comments


Related Posts


Published

Category

Programming

Tags

Contact