pyMySQL连接本地mysql8报2003错误

问题描述:
我的pymysql连接mysql的python语句是:

conn = pymysql.connect(host='127.0.0.1',port=3306, user='root', password='xxx123' ,database='astock', charset='utf8')

pymysql报2003异常
2003, “Can’t connect to MySQL server on %r (%s)” % ,就是这串错误;

给pymysql打断点发现是connections.py文件下的self._get_server_information()语句报的异常;
pyMySQL连接本地mysql8报2003错误_第1张图片
pymysql的connections.py的原码如下:

 def _connect(self):
        sock = None
        try:
            if self.unix_socket and self.host in ('localhost', '127.0.0.1'):
                sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                t = sock.gettimeout()
                sock.settimeout(self.connect_timeout)
                sock.connect(self.unix_socket)
                sock.settimeout(t)
                self.host_info = "Localhost via UNIX socket"
                if DEBUG: print('connected using unix_socket')
            else:
                while True:
                    try:
                        sock = socket.create_connection(
                                (self.host, self.port), self.connect_timeout)
                        break
                    except (OSError, IOError) as e:
                        if e.errno == errno.EINTR:
                            continue
                        raise
                self.host_info = "socket %s:%d" % (self.host, self.port)
                if DEBUG: print('connected using socket')
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            if self.no_delay:
                sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            self.socket = sock
            self._rfile = _makefile(sock, 'rb')
            self._get_server_information()
            self._request_authentication()

            if self.sql_mode is not None:
                c = self.cursor()
                c.execute("SET sql_mode=%s", (self.sql_mode,))

            if self.init_command is not None:
                c = self.cursor()
                c.execute(self.init_command)
                self.commit()

            if self.autocommit_mode is not None:
                self.autocommit(self.autocommit_mode)
        except Exception as e:
            self._rfile = None
            if sock is not None:
                try:
                    sock.close()
                except socket.error:
                    pass
            raise OperationalError(
                2003, "Can't connect to MySQL server on %r (%s)" % (self.host, e))

ctrl点击_get_server_information进入_get_server_information函数
发现是self.server_charset = charset_by_id(lang).name这条语句报的异常;
pyMySQL连接本地mysql8报2003错误_第2张图片
我在ctrl点击charset_by_id进入charset_by_id
发现pymysql添加的最大编码集是243
pyMySQL连接本地mysql8报2003错误_第3张图片
但是我的mysql返回的是255,所以报异常;

解决办法:
在mysql的my.ini文件,mysql8的my.ini文件在programData下面
pyMySQL连接本地mysql8报2003错误_第4张图片

在my.ini文件中添加
[mysqld]
port=3306
character-set-server=utf8
default_authentication_plugin=mysql_native_password
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
即可解决

你可能感兴趣的:(python程序,pymysql)