如何从python访问您网站的SQL数据(phpmyadmin)

rjjhvcjd  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(241)

我对这些东西都不熟悉
我试图从python访问我的网站数据库(phpmyadmin)。所有在互联网上可用的教程都使用xampp localhost,这是完美的工作,但当我用我的网站地址或主机ip替换localhost时,它会给出错误([Errno 11001] getaddrinfo失败)

import pymysql

db = pymysql.connect(host='localhost', user='root',passwd='')
cursor = db.cursor()
query = ("SHOW DATABASES")
cursor.execute(query)
for r in cursor:
    print(r)

输出

('indus',)
('information_schema',)
('mysql',)
('performance_schema',)
('phpmyadmin',)
('test',)
('wordpress',)

使用网站地址或域ip替换localhost会给出输出

Traceback (most recent call last):
  File "C:\Users\HYSTOU\PycharmProjects\HelloWorld\venv\lib\site-packages\pymysql\connections.py", line 613, in connect
    sock = socket.create_connection(
  File "C:\Users\HYSTOU\AppData\Local\Programs\Python\Python39\lib\socket.py", line 823, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\HYSTOU\AppData\Local\Programs\Python\Python39\lib\socket.py", line 954, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\HYSTOU\PycharmProjects\HelloWorld\indusExtractingFloat.py", line 26, in <module>
    db = pymysql.connect(host='https://www.rubick.org/', user='root',passwd='')
  File "C:\Users\HYSTOU\PycharmProjects\HelloWorld\venv\lib\site-packages\pymysql\connections.py", line 353, in __init__
    self.connect()
  File "C:\Users\HYSTOU\PycharmProjects\HelloWorld\venv\lib\site-packages\pymysql\connections.py", line 664, in connect
    raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'https://www.rubick.org/' ([Errno 11001] getaddrinfo failed)")

使用其他方法给出(超时)

hgb9j2n6

hgb9j2n61#

定义主机连接时,基本上就是说要连接到Web上的给定节点。如果host的值为localhost,则意味着应用程序将连接到同一台计算机的数据库。
是的,你也可以连接到一个远程主机,但这并不像你想的那么容易。你肯定会遇到一个问题,那就是你把主机的值设置为https://www.rubick.org/,这是不正确的,因为HTTPS是一种你可以用来从浏览器访问资源的协议,但它不是主机名的一部分。试试www.rubick.org
假设主机名正确,您可能还需要执行一些进一步的步骤,以确保此连接成功,例如连接到VPN或您的系统管理员可能指定您需要执行的任何其他必要步骤。

相关问题