Can`t connect MySQL database in phpMyAdmin by python - python

I need to use python to connect database in phpMyAdmin.
import MySQLdb
db = MySQLdb.connect(host="10.0.0.140",port=80,user="root",passwd="password")
cursor=db.cursor()
cursor.execute("SHOW DATABASES")
results=cursor.fetchall()
for result in results:
print row
and I get this error
2013,"Lost connection to MySQL server at'waiting for initial communication packet',system error:0"
I can access the database in chrome, so I don`t think it is a problem of remote access.
=======================update=======================
The real reason is that I am in a limited net segment.(maybe)
There is something wrong with SQL connection out of my code.
If changed code like this:(port is not needed)
MySQLdb.connect(host="10.0.0.140",user="root",passwd="password")
People in public net segment can connect the phpMyAdmin but I can`t.
I can not change my net segment so I can`t confirm it is the real reason.
But it is the only difference between me and others.

Check the connection, are you sure you run your mysql at port 80?
As I wrote in the comments, I doubt it runs port 80, this is the url to your phpMyAdmin and not the core mysql database server. Normally the web and database run on different machines, different IP addresses etc. Is this a hosted environment? Or do you run this in your local machine.
If it is your local machine then changing to port 3306 should work.
If it is a hosted environment by a hosting partner then you need to check the JDBC url from them. As you wrote it is XXXX hosted partner. Then check your XXX and it will provide you a XXX jdbc url for your mysql, and be sure to grant access to the user so you can logon to the mysql from your workstation remotly. The mysql url/host could be something like this NNNN-aaaa-bbbb-cccc-dddd.xxx.domain.xx
Also did you try the PyMYSQL
import pymysql
db = pymysql.connect(host='10.0.0.140',user='root',passwd='password')
cursor = db.cursor()
query = ("SHOW DATABASES")
cursor.execute(query)
for r in cursor:
print r

This is not the way you should do it. phpMyAdmin serves HTML to the user and should be used in a Web browser. You should connect directly to the MySQL host which is usually listening on port :3306. Also, you should keep in mind that, in production, MySQL servers are ordinarily not listening on public interfaces.

Related

Cannot connect AS400 by python

I am using idb_bm module to build the connection to the AS400 by using the following code.
from ibm_db import connect
connection = connect('DATABASE=DATABASE;'
'HOSTNAME=xxx.xxx.xxx.xxx;'
'PORT=446;'
'PROTOCOL=TCPIP;'
'UID=uid;'
'PWD=pwd;', '', '')
After execute the code, it shows the following error,
connection = connect('DATABASE=DATABASE;'
SQLCODE=-30061][CLI Driver] SQL30061N The database alias or database name "DATABASE " was not found at the remote node. SQLSTATE=08004
The AS400 structure is complicated with tons of library and tons of table in each library. What's the database alias or database name actually? I have stuck to it for few days....Thanks all.
Assuming you actually have a modern POWER server running IBM i and not a 20 year old AS/400...
Download IBM Access Client Solutions (ACS)..
Set up a connection to your IBM i.
The "Schemas" component of ACS will show you a list of databases on the server.
Assuming you're connecting to the local database and not an iASP, you should be able to use *LOCAL.

Python using mysql.connector.connect yields an Interface Error

When I execute the code
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="user",
passwd="password"
)
I get:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)
I've tried using an example with "pymysql' and the error stays.
I looked around the internet and a lot of people say it could be a firewall inbound problem. Yet there is no fire wall stopping 3306. The security group on Amazon RDS allows all connections. I connected the RDS instance to my local mySql Workbench (so I can make tables and stuff from there). Interestingly enough when I run the code and the error persists there is additional client connections that pop up. Anyone else deal with this? Thank you very much I'm trying to learn this part of AWS well.
I figured it out. My host name should NOT be "localhost" it should be my endpoint on Amazon RDS. It seems clear now considering when I connected my RDS database to mySql workbench I put in "databasename.xxxxxxx.us-east-1.rds.amazonaws.com" for my host name. Now I can connect and read/write to the database.

Extract data from client database

I have to send multiple data through Python from a DB allocated in the client to the Main DB in the server, whats the best solution to this? i currently have my web server up and functioning, i can fill my DB locally but i dont really know how to do it remotely, im using Python in my hardware, here is what i have so far in the client:
import mysql.connector
cnx = mysql.connector.connect(user='user', password='pass', host='url?', database='db') #im able to enter with this
cursor = cnx.cursor()
query = ("INSERT INTO IGNORE table " "(id,date,son) " "VALUES( (%s,%s,%s)")
for row in data: #ive already extracted data from the other DB
cursor.execute(query, (row[0],row[1],row[2]))
wich yealds an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the righ syntax to use near 'IGNORE table(id,date,son) VALUES (number, '2017-11-09 14:33:15', 18.987)' at line 1
One possibility - Make sure your mysqld service is binding to your external ip address (set in your mysql config file on the server). By default I believe it binds to 127.0.0.1 or localhost. If it is binding to localhost, your db will never respond to external requests.
I fixed the issue with the MySQL port in the following way: I opened the port internally from my VM instead of from my Virtual Network (I'm using the Google Cloud Platform).

Allow remote access to Database

My problem: My team is writing a program in python and, locally, I have a db (let's call it test.db) on SQL SERVER. So I have the following code:
connection = pypyodbc.connect( "DRIVER={SQL Server};SERVER=localhost;trusted_connection='yes';DATABASE=test")
My issue now, is my teammates need to use my Database. All I've found online is how to allow access to my DB through Management Studio ( so allow TCP/IP and allow port 1434)
I would like to have something like this
connection = pypyodbc.connect( "DRIVER={SQL Server};SERVER="My ip address" ;trusted_connection='yes';DATABASE=test")
Is this possible? ( where my ip address = some kind of number)
I was thinking of installing a server that listens for incoming connections but I've never really done this so I was wondering if there was another way to go about this.

OpenShift mysql connection issues in python

I previously wrote my app using local development servers, and now that I have moved it onto an openshift small gear almost all works except for mysql connections.
In my code I have the line:
self.db = MySQLdb.connect(host, username, password, dbname)
When I review the openshift error log, the following error is reported:
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
I think that python is trying to connect using a UNIX socket as opposed to an INET one, but I'm not sure how to change this behavior. Any help is much appreciated.
Not specific to MySQLdb: if you use localhost as hostname, a MySQL client using the MySQL C libraries will try to connect using UNIX socket (or named pipe on Windows). There are 2 ways around this, but you'll need to grant extra permissions to make it work for both:
Use IP address 127.0.0.1
Use IP address 127.0.0.1 instead of the localhost hostname. This will make MySQL client connect using TCP/IP.
Use option files
The other way is to force the protocol using using option files. For example, in your ~/.my.cnf (or any file you want), add the following:
[python]
protocol=tcp
Now use the connection arguments to read the option file and group:
import MySQLdb
cnx = MySQLdb.connect(host='localhost', user='scott', passwd='tiger',
read_default_file='~/.my.cnf',
read_default_group='python')
The group name does not need to be python, but it is good not to use mysql or client as it might interfere with other MySQL tools (unless you want that of course).
For setting up permissions, you'll need to use the IP address of localhost, something like:
mysql> GRANT SELECT TO yourdb.* TO 'scott'#'127.0.0.1' IDENTIFIED BY ...;
(Site note: MySQL database drivers such as MySQL Connector/Python do not consider localhost to be special and connect through TCP/IP right away and you have to explicitly use the unix_socket.)
As I later discovered, while the database server runs on localhost, it runs on a very specific localhost bind address. In my case it was an address that I would never have though to try if I hadn't noticed how phpmyadmin was connecting.

Categories

Resources