Python报错-(2003 Can t connect to MySQL server on 127.0.0.1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/su749520/article/details/78940096

报错修复:bpymysql.err.OperationalError: (2003, “Can’t connect to MySQL server on ‘127.0.0.1’”)

报错日志

D:\python3-webapp-Su\www>python app.py
INFO:root:建立数据库连接池
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\connection.py", line 476, in _connect
    yield from self._request_authentication()
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\connection.py", line 702, in _request_authentication
    auth_packet = yield from self._read_packet()
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\connection.py", line 567, in _read_packet
    packet.check_error()
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\connections.py", line 384, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.InternalError: (1049, "Unknown database 'sufadi'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app.py", line 79, in <module>
    loop.run_until_complete(init(loop))
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 467, in run_until_complete
    return future.result()
  File "app.py", line 63, in init
    yield from orm.create_pool(loop = loop, host = "127.0.0.1", port = 3306, user = "root", password = "", database = "sufadi")
  File "D:\python3-webapp-Su\www\orm.py", line 29, in create_pool
    loop = loop
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\utils.py", line 70, in __iter__
    resp = yield from self._coro
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\pool.py", line 30, in _create_pool
    yield from pool._fill_free_pool(False)
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\pool.py", line 173, in _fill_free_pool
    **self._conn_kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\utils.py", line 70, in __iter__
    resp = yield from self._coro
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\connection.py", line 78, in _connect
    yield from conn._connect()
  File "C:\ProgramData\Anaconda3\lib\site-packages\aiomysql\connection.py", line 496, in _connect
    self._host) from e
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1'")

原因就是连接不上 MySQL服务器

检查 是否启动

先验证一下MySQL的服务是否开启,到计算机->管理->服务和应用程序->服务
这里写图片描述

启动方法

右键启动 或者

C:\WINDOWS\system32>net start mysql

查看客户端和服务端的服务器和端口是否一致

Mysql服务端的my.ini,端口是 3306

(安装路径的根目录D:\mysql-5.6.24-win32)

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

[mysqld]

bind-address = 127.0.0.1
#设置 3306 端口
port = 3306
# 设置mysql的安装目录
basedir=D:\mysql-5.6.24-win32
# 设置mysql数据库的数据的存放目录
datadir=D:\mysql-5.6.24-win32\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
#代码结束

配置参数每次修改,都需要重新启动 mysql

计算机->管理->服务和应用程序->服务->mysql 右键,重新启动即可

客户端的端口

    yield from orm.create_pool(loop = loop, host = "127.0.0.1", port = 3306, user = "root", password = "", database = "test")

具体如下

@asyncio.coroutine
def init(loop):
    yield from orm.create_pool(loop = loop, host = "127.0.0.1", port = 3306, user = "root", password = "", database = "test")

    #创建一个web服务器对象
    app = web.Application(loop = loop)
    #通过router的指定的方法可以把请求的链接和对应的处理函数关联在一起
    init_jinja2(app, filters=dict(datetime = datetime_filter))
    add_routes(app, "handlers")
    add_static(app)
    #运行web服务器,服务器启动后,有用户在浏览器访问,就可以做出对应的响应
    # 127.0.0.1 本机地址
    srv = yield from loop.create_server(app.make_handler(), "127.0.0.1", 9000)
    logging.info("服务端 http://127.0.0.1:9000....")
    return srv

# 固定写法
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()

运行效果

D:\python3-webapp-Su\www>python app.py
INFO:root:建立数据库连接池
INFO:root:初始化 jinja2
INFO:root:设置 jinja2 templates 地址为 D:\python3-webapp-Su\www\templates
INFO:root:add_static /static/ -> D:\python3-webapp-Su\www\static
INFO:root:服务端 http://127.0.0.1:9000....
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页