Hi I'm trying to connect from python to MySQL but I can't apparently due to below error. I tried it with this...
import MySQLdb
cnx = MySQLdb.connect(user ='phpmyadmin',
passwd ='raspberry',
host ='192.168.0.58',
database ='freddy')
print 'connected'
cnx.close()
and then i changed "Mysqldb" to "Mysql.connector" but doesnt work. This is the error showing :
Traceback (most recent call last):
File "test.py", line 2, in
cnx = MySQLdb.connect(user='phpmyadmin',passwd='raspberry',host='192.168.0.58',database='freddy')
File "/usr/lib/python2.7/dist-packages/MySQLdb/init.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 204, in init
super(Connection, self).init(*args, **kwargs2)
TypeError: 'database' is an invalid keyword argument for this function
....
what should i do? i know my password its fine, but must be somethig else...
The word database should be db and then you should be fine and connect.
Change your loginpart into below code:
cnx = MySQLdb.connect(user ='phpmyadmin',
passwd ='raspberry',
host ='192.168.0.58',
db ='freddy')
This gives you oversight of used keys and values. Enjoy ;-)
Related
I am trying to connect Python to a web server's database, 000webhost I am using. Here is my code:
import MySQLdb
>>> conn = MySQLdb.connect(host="mysql9.000webhost.com",user="user_name",passwd="password",db="db_name")
I think I install MySQLdb successfully. And all the username, password, host, database name are exactly the same as they are in my php file, which works perfectly. However, I got this error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
conn = MySQLdb.connect(host="mysql9.000webhost.com",user="a6969519_123",passwd="lianshiyu08",db="a6969519_shiyu")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'mysql9.000webhost.com' (60)")
What did I miss?
The database is only reachable from the internal web of your hoster.
I am trying to connect to a MySQL database using python but I am getting a strange error. It is compounded by the fact that I can use the same connection values from the mysql console command and it connects with no problems.
Here is the exact code I am using:
import pymysql
from checks import AgentCheck
class DelayedJobCheck(AgentCheck):
def check(self, instance):
self.log.info("testing connection")
self.log.info(instance)
connection = pymysql.connect(**instance)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()
self.gauge('hello.world', 1)
This is the error that I am getting:
Traceback (most recent call last):
File "/opt/datadog-agent/agent/checks/__init__.py", line 661, in run
self.check(copy.deepcopy(instance))
File "/opt/datadog-agent/agent/checks.d/delayed_job.py", line 10, in check
connection = pymysql.connect(**instance)
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/pymysql/__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/pymysql/connections.py", line 644, in __init__
self._connect()
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/pymysql/connections.py", line 869, in _connect
raise exc
OperationalError: (2003, u"Can't connect to MySQL server on '192.168.199.86' ([SSL: SSL_NEGATIVE_LENGTH] dh key too small (_ssl.c:590))")
I am running this code on a Ubuntu box and I though initially that it might be because the SSL CA is a self generated cert. So I followed the steps here But, it did not make any difference. Also I have verified that the process that is running this code has full access to the cert files
Any ideas what else might be causing this?
As the err info said dh key is too small, a larger one might help. Replace the default dh512.pem file with dh4096.pem
sudo wget "https://git.openssl.org/gitweb/?p=openssl.git;a=blob_plain;f=apps/dh4096.pem" -O dh4096.pem
Ref: http://www.alexrhino.net/jekyll/update/2015/07/14/dh-params-test-fail.html
I am newbie to python,I have simple code to connect database using MySQLdb and Python3.4 running in localhost.
Python Code:
#!c:/Python34/python.exe -u
import cgitb ,cgi
import sys
import MySQLdb
conn = MySQLdb.connect(host='localhost',port=3306,
db='example2',
user='root',
password='tiger')
cursor = conn.cursor()
if conn:
print("sucess")
But getting error ,while executing a code
Error:
Traceback (most recent call last):
File "<pyshell#27>", line 4, in <module>
password='tiger')
File "C:\Python34\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python34\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required (got type str)
Please give some suggestion
You need to remove the single quotes from the port.
conn = MySQLdb.connect(host='localhost',
port=3306,
database='example2',
user='root',
password='tiger')
I am newbie to python ,started with implementing small programs ,now trying with connecting database .I installed python 3.4 and mysql.connector 2.0.4 .
Below given code is what i used to connnect the database
#!"C:\python34\python.exe"
import sys
import mysql.connector
print("Content-Type: text/html;charset=utf-8")
print()
conn = mysql.connector.connect(host='localhost:8051',
database='test',
user='root',
password='tiger')
if conn.is_connected():
print('Connected to MySQL database')
But i am getting error as given below . not getting why this error are occurring ,because of setup environment is wrong or of some other reason
Please suggest
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 448, in open_connection
socket.SOL_TCP)
File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#8>", line 2, in <module>
password='tiger',database='test')
File "C:\Python34\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Python34\lib\site-packages\mysql\connector\abstracts.py", line 719, in connect
self._open_connection()
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 209, in _open_connection
self._socket.open_connection()
File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 464, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:8051:3306' (11004 getaddrinfo failed)
The error message is quite clear:
Can't connect to MySQL server on 'localhost:8051:3306'
See the double port definition?
host='localhost:8051'
should most likey be
host='localhost', port='8051'
You are using the connection paramater wrong. I think you need to do this:
conn = mysql.connector.connect(host='localhost',
port=8051,
database='test',
user='root',
password='tiger')
Check out this doc for more detail.
The default Port for MySQL is 3306 , when you don't pass "port" argument to mysql.connector.connect it assumes you are using default port which is 3306 , if you use another port you should pass :
port=xxxx
to the constructor.
For me, I added all connector on my IDE.
mysql-connector
mysql-connector-async-dd
mysql-connector-python
mysql-connector-python-dd
mysql-connector-repackaged
then you can see your all database names here:
import mysql.connector
mydb = mysql.connector.connect(host='localhost',user='root', passwd = 'Root#1234')
mycursor = mydb.cursor()
mycursor.execute("show databases")
for i in mycursor:
print(i)
I'm having trouble connecting to a db on a new system. The version of Python is the same. Here is the connection string for the DB that is there and you'll see the error message change, but I cannot work out why when the connection string is correct I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pg8000/__init__.py", line 148, in connect
user, host, unix_sock, port, database, password, socket_timeout, ssl)
File "/usr/local/lib/python2.7/dist-packages/pg8000/core.py", line 1157, in __init__
raise exc_info()[1]
TypeError: cannot concatenate 'str' and 'int' objects
Here is my connection string when correct:
conn = DBAPI.connect(host='sql2', user='XXX', password='XX', database='XX', socket_timeout=100, port=5432)
and when I change to something incorrect:
conn = DBAPI.connect(host='sql2', user='XXX', password='XX', database='XX', socket_timeout=100, port=100)
and the following error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/mnt/opt/Centos5.8/python-2.7.4_scipy0.13/lib/python2.7/site-packages/pg8000/__init__.py", line 148, in connect
user, host, unix_sock, port, database, password, socket_timeout, ssl)
File "/mnt/opt/Centos5.8/python-2.7.4_scipy0.13/lib/python2.7/site-packages/pg8000/core.py", line 854, in __init__
raise InterfaceError("communication error", exc_info()[1])
pg8000.errors.InterfaceError: ('communication error', error(111, 'Connection refused'))
I cannot figure out where this error message is coming from - it seems bizzare. I've tried:
wrapping string in str()
putting u'ss'
removing port, adding socket-timeout etc etc.
When I looked in the core.py code from pg8000 it appears that this comes from an authentication error if you chase it through. I think this error (int and str) might be eclipsing the authentication error.
Message codes
AUTHENTICATION_REQUEST = b("R")
This is a bug in pg8000. It was fixed in pg8000-1.9.11. The bug is an error in reporting the error that pg8000 encountered while trying to authenticate against the database.