Mysql.connector to access remote database in local network Python 3

2024/10/14 11:19:55

I used mysql.connector python library to make changes to my local SQL server databases using:

from __future__ import print_function
import mysql.connector as kkcnx = kk.connect(user='root', password='password123',host='localhost',database='db')
cursor = cnx.cursor(buffered=True)
sql = "DELETE FROM examples WHERE id = 4"
number_of_rows = cursor.execute(sql)
cnx.commit()   
cnx.close()

This works fine, but when i try the same code with a change only to the 'host' parameter, with something like,

host='xxx.xxx.xxx.xxx'

(where the IP is that of a server connected to my local network.), it won't update that particular data base in that server.

The error thrown is something like:

mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'xx.xxx.x.xx' (10060)

Why wouldn't this work?

Answer

First, you must check if your local IP can acces to your remote server (check if you are an IP restriction on your server), after check if your mysql database use the default port or not, If not you must precise the port in your code.

https://en.xdnf.cn/q/117964.html

Related Q&A

concurrent.futures not parallelizing write

I have a list dataframe_chunk which contains chunks of a very large pandas dataframe.I would like to write every single chunk into a different csv, and to do so in parallel. However, I see the files be…

Querying SQLite database file in Google Colab

print (Files in Drive:)!ls drive/AIFiles in Drive:database.sqlite Reviews.csv Untitled0.ipynb fine_food_reviews.ipynb Titanic.csvWhen I run the above code in Google Colab, clearly my sqlite file is pre…

AttributeError: function object has no attribute self

I have a gui file and I designed it with qtdesigner, and there are another py file. I tried to changing button name or tried to add item in listwidget but I didnt make that things. I got an error messa…

Find file with largest number in filename in each sub-directory with python?

I am trying to find the file with the largest number in the filename in each subdirectory. This is so I can acomplish opening the most recent file in each subdirectory. Each file will follow the namin…

Selenium Python - selecting from a list on the web with no stored/embedded options

Im very new to Python so forgive me if this isnt completely comprehensible. Im trying to select from a combobox in a webpage. All the examples Ive seen online are choosing from a list where the options…

How to use a method in a class from another class that inherits from yet another class python

I have 3 classes :class Scene(object):def enter(self):passclass CentralCorridor(Scene):def enter(self):passclass Map(object):def __init__(self, start_game): passAnd the class map is initiated like this…

Finding common IDs (intersection) in two dictionaries

I wrote a piece of code that is supposed to find common intersecting IDs in line[1] in two different files. On my small sample files it works OK, but on my bigger files does not. I cannot figure out wh…

Run command line containing multiple strings from python script

Hello i am trying to autogenerate a PDF, i have made a python script that generates the wanted PDF but to generate it i have to call my_cover.py -s "Atsumi" -t "GE1.5s" -co "Ja…

Identify value across multiple columns in a dataframe that contain string from a list in python

I have a dataframe with multiple columns containing phrases. What I would like to do is identify the column (per row observation) that contains a string that exists within a pre-made list of words. Wi…

ipython like interpreter for ruby

I come from python background and am learning ruby. IPython is really awesome. I am new to ruby now, and wanted to have some sort of ipython things. As of now am having tough time, going along ruby lin…