简体   繁体   English

mysql.connector 在尝试连接到本地主机上的数据库时返回错误

[英]mysql.connector is returning an error when trying to connect to database on localhost

The following is the python script I am trying to run to connect to a database and it is throwing an error.以下是我试图运行以连接到数据库的 python 脚本,但它正在引发错误。

Code:代码:

import mysql.connector as mariadb

DB_USER = 'username'
DB_PASS = 'password'
DB_NAME = 'databaseName'

#DATABASE Connection
DB = mariadb.connect(host='localhost',port=3306,database=DB_NAME,user=DB_USER,password=DB_PASS)

cursor = DB.cursor()

Error:错误:

Traceback (most recent call last):
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/pymysql/connections.py", line 583, in connect
    **kwargs)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "importIntoDB.py", line 27, in <module>
    on = pymysql.connect(host='localhost',user='DB_USER',password='DB_PASS',db='DB_NAME',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/pymysql/connections.py", line 325, in __init__
    self.connect()
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/pymysql/connections.py", line 630, in connect
    raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

My process so far:到目前为止我的过程:

1. I verified that the login details are correct by logging in manually 1.我通过手动登录验证了登录信息是正确的

2. I tried a second library for mysql connections (PyMySQL) and it throws the same error 2.我为 mysql 连接(PyMySQL)尝试了第二个库,它抛出了同样的错误

Any ideas on how to get this working?关于如何使它工作的任何想法?

Did you already try indicate the ip ( 127.0.0.1 ) instead of "localhost"您是否已经尝试指示 ip ( 127.0.0.1 ) 而不是"localhost"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM