当前位置: 首页 > 知识库问答 >
问题:

拒绝用户在python docker容器中连接远程MySQLdatabase的访问

孔安阳
2023-03-14

以下是我的dockerfile:

# Docker image to build the container from 
FROM python:3 

# Path to script
ADD DLinvoices.py /



#Download the Mysql library

RUN pip install mysql-connector-python

#Download requests library
RUN pip install requests

#Running the script
CMD [ "python", "./DLinvoices.py" ]

下面是我运行docker映像时的控制台输出:

$docker run docker_invoice
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 179, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Access denied for user 'user'@'ip.isp.overthebox.ovh' (using password: YES)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./DLinvoices.py", line 15, in <module>
    cnx = mysql.connector.connect(user='user', password='***', host='ip_host', database='dbName')
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/__init__.py", line 172, in connect
    return CMySQLConnection(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 78, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 736, in connect
    self._open_connection()
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 182, in _open_connection
    sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'user'@'ip.isp.overthebox.ovh' (using password: YES)

编辑:python脚本

import mysql.connector
import datetime
import requests
import io
import xmlrpc.client
import base64
import shutil
import errno

__PATH_TO_NAS__ = "./"

# DataBase connection
try:
    cnx = mysql.connector.connect(user='user', password='pwd', host='ip_host, database='dbname')
    cursor = cnx.cursor(buffered=False, dictionary=False)
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)

## variables ##

dtnow = datetime.datetime.today()
dtnow_tt = dtnow.timetuple()
day = dtnow_tt.tm_mday
month = dtnow_tt.tm_mon
year = dtnow_tt.tm_year

listId = []

## queries ##

query = "SELECT id_order, YEAR(invoice_date) year FROM ps_orders WHERE YEAR( invoice_date ) = " + str(
    year - 1) + " AND MONTH( invoice_date ) = " + str(month) + " AND DAY( invoice_date ) = " + str(day - 1)

cursor.execute(query, year)

##
#recuperation des IDs des factures a imprimer
##

for (id_order, year) in cursor:
    listId.append(id_order)
cursor.close()
cnx.close()



for id in listId:
    data = {"id": id}
    r = requests.post("link_to_the_pdf_file", data=data)
    print(r.headers['Content-type'])
    r.raw.decode_content = True
    try:
        with open(str(id) + "F.pdf", 'wb') as f:
            f.write(r.content)

    except IOError:

        print("Erreur! Le fichier n\' pas pu être ouvert ou est deja existant")

共有1个答案

戈博易
2023-03-14

第一件事,如果不是错字

cnx = mysql.connector.connect(user='user', password='pwd', host='ip_host', database='dbname')

MySQL连接字符串中缺少'

我很确定这个错误不是来自docker,下面是确认它的方法。

docker run --rm --name mariadb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=hellopass -it mariadb:10.4
FROM python:3.7.4-alpine3.10
RUN pip install mysql-connector

RUN echo $'#!/usr/bin/python \n\
import mysql.connector \n\
try:\n\
    cnx = mysql.connector.connect(user=\'root\', password=\'hellopass\', host=\'dbhost\') \n\
    cursor = cnx.cursor(buffered=False, dictionary=False) \n\
except mysql.connector.Error as err: \n\
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: \n\
        print("Something is wrong with your user name or password") \n\
    elif err.errno == errorcode.ER_BAD_DB_ERROR: \n\
        print("Database does not exist") \n\
    else: \n\
        print(err) \n\
print(cnx.is_connected())' >> /root/dbtest.py 
CMD [ "python" , "/root/dbtest.py"]
 docker run --rm --link mariadb:dbhost -it pymsql ash -c "python /root/dbtest.py"
 类似资料:
  • 我使用最新版本的PsExec,并以管理员身份运行执行命令提示符。我们使用PsExec v2.11 执行简单的服务器连接测试(例如从我的机器my_机器到远程_服务器) 另一个域中的另一个伙伴,它获取拒绝访问错误。 要登录到计算机的用户是域\u伙伴\用户\u伙伴。 我检查了PSExec使用的端口445和135,它们都在远程_服务器上打开。其他领域。本地计算机: 可从资源管理器访问目录: 我得到访问被拒

  • 问题内容: 在Windows XP上运行的MySQL 5.1.31。 从 本地 MySQL服务器(192.168.233.142),我可以以root用户身份进行如下连接: 从 远程 计算机(192.168.233.163),我可以看到mysql端口已打开: 但是,当尝试从 远程 计算机连接到mysql时,我收到: 我在mysql.user中只有2个条目: 我还需要做些什么才能启用远程访问? 编辑

  • 我花了几个小时在谷歌上搜索,但似乎找不到正确的路径/文档来帮助我走上正确的道路:( 前提很简单。 我有一个在localhost:8080上打开的springboot应用程序。我有一个在localhost:15672上打开的rabbitmq服务器 当这两个应用程序都运行时,springboot应用程序将通过一些用户交互向Rabbitmq服务器发送消息。当我对这两个独立的服务进行dockerize(容

  • 试图在localhost中建立从app容器到mysql容器的连接,出现连接拒绝异常 我们正在采取一种docker的方法来调用rest api服务来采用微服务的方法。我们正在建立应用程序容器和mysql容器之间的连接,同时我们编写了一个docker-compose文件,创建了mysql容器和应用程序容器,为这两个容器公开了端口。下面是运行docker-compose文件docker-compose

  • 这是我用来创建数据库的php脚本,我正在传递rootname=root和rootpass=toor 但我得到一个错误 警告:mysql_connect():第17行/var/www/webdefender/script/dbcreate.php中用户“root”@“localhost”(使用密码:否)的访问被拒绝无法连接:用户“root”@“localhost”的访问被拒绝(使用密码:否) 但是当