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.
Related
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 ;-)
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've read everything possible and I just can't connect to my mysql server. Here is my code. very simple. (I used xxx to hide all the private info. everything else is the way i got it.
import mysql.connector
conn = mysql.connector.connect(user='xxxx',password='xxxxx',host='xxxx',db='xxx',port=3306)
The login info is definitely correct. I tried it in a mysql client and it connected just fine. The MySQL server is hosted on dreamhost. I set the allowable IPs for the db user to wildcard %.%.%.% so anyone could connect.I don't know what else could possibly be wrong. When I try to connect in python i get:
C:\Python33\python.exe D:/Dropbox/python/v2/test.py
Traceback (most recent call last):
File "D:/Dropbox/python/v2/test.py", line 3, in <module>
conn = mysql.connector.connect(user='xxx',password='xxx',host='xxx',db='xxx',port=3306)
File "C:\Python33\lib\site-packages\mysql\connector\__init__.py", line 101, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 117, in __init__
self.connect(**kwargs)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 383, in connect
self._open_connection()
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 350, in _open_connection
self._ssl)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 176, in _do_auth
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1044 (42000): Access denied for user 'xxx'#'%.%.%.%' to database 'xxx'
Any help would be greatly appreciated.
You will have to GRANT permissions to the user.
Something like:
GRANT ALL PRIVILEGES ON *.* TO 'user2'#'localhost' IDENTIFIED BY PASSWORD 'xxxxx'
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.