import MySQLdb
conn = MySQLdb.connect('localhost', 'user', 'pwd', 'sampledb')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (110)")
How do I connect and what is the issue here? Thank you!
You do not connect to a database on localhost on Go Daddy shared hosting. You will need to locate the correct database host.
Log in to your Account Manager.
Click Web Hosting.
Next to the hosting account you want to use, click Launch.
In the Databases section of the Hosting Control Center, click the MySQL icon.
Click the pencil icon next to the database you would like to get connection strings for.
Your database host name displays in the Host Name field. It will end with "hostedresource.com".
See godaddy's help pages:
http://support.godaddy.com/help/39
import MySQLdb
conn = MySQLdb.connect("host", "user", "password", "database")
host: Get in cPanel "Web sites IP".
user: This user must to have permissions to access (You can add this permission in MySQL Databases menu in cPanel).
database: Name database.
Note: Add permission for your IP in Remote MySQL Menu in cPanel
You can use sqlalchemy
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://user:password#host/database')
conn = engine.connect()
Related
Developing a desktop application with tkinter, I had an error connecting the database to it.
I'm new to this, I was working locally but I don't know what would happen if I want to share my app with other people. The app would not have access to this database.
I tried to create a database in render.com, once created I made the connection to it through psycopg2 passing the data (hostname, port, db_name, username, and password), but it throws me the error psycopg2:OperationalError.
I know that I am doing something wrong and I have been investigating but I have not been able to find the solution to this. So, when deploying a tkinter app, what database could it use? so that everyone can use the app correctly
I would appreciate any help you can give me, thanks in advance.
Here are the connection settings, which are the same settings from the database created on render.com (I've hidden the password and host):
conn = psycopg2.connect(
dbname='tkinter_app',
user='tkinter_app_user',
password='#############',
host='dpg-cfkimi9mbjsn9eclo3dg-a',
port='5432',
)
And here is the error log:
Traceback (most recent call last):
File "c:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\src\student.py", line 134, in <module>
display_students()
File "c:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\src\student.py", line 27, in display_students
conn = psycopg2.connect(
File "C:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "dpg-cfkimi9mbjsn9eclo3dg-a" to address: Unknown host
When I created the database, I copied all the configurations as they were, investigating I read that the host should be the External Database URL showed in the rander database configuration, but it didn't work either and it also throws me the error: Unknown Server error
I also try to connect the database in pgAdmin but throws me the same error Unknown host
Python 3.8
Mysql 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
Hi,
I want to connect to a distant mysql server using python's mysqldb connector and paramiko's sshtunnelforwarder.
I can connect to the database remotely without any problems by executing the following:
Connecting to database using mysql password authentication
server = new_ssh_server(config)
with server:
print('Connection', server.local_bind_address)
cnx = MySQLdb.connect(host = '127.0.0.1',
port = server.local_bind_port,
user = config['user'],
passwd = config['password'],
db = config['db'])
Queries work, I can read/write to database, no problem.
I would like to connect to database without supplying mysql password, by using mysql auth_socket authentication method, through ssh.
My attempts at this can be resumed by the following code:
Connecting to database using mysql auth_socket authentication
with server as tunnel:
print('Tunnel:', tunnel.local_bind_address)
cnx = MySQLdb.connect(host = 'localhost', user = 'hillbilly', password = '', db='tutut')#, unix_socket="/tmp/mysql.sock")
res = pd.read_sql('select * from users;', cnx)
print(res)
Which throws the following error:
File "connect_ssh_mysql_auth_socket.py", line 12, in <module>
cnx = MySQLdb.connect(host = 'localhost', user = 'hillbilly', password = '', db='rsotest2')#, unix_socket="/tmp/mysql.sock")
File "...../lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "...../lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
I have and existing mysqld.sock on the distant server that I symlinked to /tmp/mysql.sock, but the error remains. I have also added the next line to /etc/mysql/mysql.conf.d/mysql.cnf:
socket=/run/mysqld/mysqld.sock
But I still get the same error when trying to connect remotely.
Specifying the unix_socket='/run/mysqld/mysqld.sock' to mysqldb connector (commented in Connecting to database using mysql auth_socket authentication) does not fix the issue.
I seem to misunderstand the use of mysql.sock, mysqld.sock.
I was not able to find nor create a mysql.sock socket.
Is what I am trying to do possible? I remember reading somewhere that unix sockets only work locally, does this mean it is not achievable?
Any help/explanation would be appreciated.
(EDIT AND CLOSING)
So this is not possible. Following this thread, auth_socket needs local access to the socket file (usually /tmp/mysql.sock) to run autentication tests, so not accessible through ssh tunneling.
Authentication to remote mysql server using auth_socket plugin is not possible through sshtunnel, as the plugin requires local access to the socket file. See this thread for more information.
I have a database in mysql and I want to connect to it. I am trying to use this module from Python called MySQLdb. I created an user (called abc) and password (abc) for this database (abc) that has one table and it is connecting ok (when I connect by mysql command line).
But when I run my python script there is an error in the connection.
My script is:
#!/usr/bin/python
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","abc","abc","abc")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data
# disconnect from server
db.close()
My error is:
Traceback (most recent call last):
File "./test.py", line 8, in <module>
db = MySQLdb.connect("localhost","abc","abc","abc")
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user 'abc'#'localhost' to database 'abc'")
What is wrong? My script or something in my mysql?
I changed my localhost to 127.0.0.1 (as suggested in another post, but did not solve my issue.
I also checked my permissions for this mysql user:
SHOW GRANTS;
+------------------------------------------------------------------------------+
| Grants for abc#localhost |
+------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'abc'#'localhost' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, CREATE VIEW ON `abc`.* TO 'abc'#'localhost' |
+------------------------------------------------------------------------------+
You might need to restart the mysql daemon for the privileges to take affect. Or use the flush privileges command. https://dev.mysql.com/doc/refman/5.7/en/privilege-changes.html
i have created a database in the database tab in pythonanywhere
and now i am trying to connect to it so i can get my tables and information from it.
what is the best way to connect to it and how !?(sqlite3 , SQLAlchemy)
i used
import import MySQLdb
db = MySQLdb.connect(host="mysql.server",
user="username",
passwd="password",
db="db-name")
and it gave me
return Connection(*args, **kwargs)
super(Connection, self).__init__(*args, **kwargs2)
mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host 'mysql.server' (0)")
After creating database in pythonanywhere .
How to connect it with python?.
This is python code in which you are going to connect to the database..
Import MySQLdb as my
db = my.connect ('database-server-name','username-of -db','password-of-db','database-name')
cursor = db.cursor ()
Now your database is connected with your server and python code
Hope this clears!!!
You will get the db server name, username, and the password you created after making database and your database name from the database tab!!
f_dbI am trying to connect to mysql database using a python script. My code to do so is the following:
`db = MySQLdb.connect(host="xxx.xx.xx.xx", # your host, usually localhost
port = xxxx,
user="chrathan", # your username
passwd="...", # your password
db="f_db") # name of the data base`
I ve open the database from mysql workbench. However, I am not able to open it from python I am getting the following error:
_mysql_exceptions.OperationalError: (1044, "Access denied for user 'chrathan'#'%' to database 'f_db'"). Basically I am receiving `Traceback (most recent call last):
File "D:\_WORKSPACE\mysql\fashion_db.py", line 7, in <module>
db = 'f_db') mysql_exceptions.OperationalError: (1044, "Access denied for user 'chrathan'#'%' to database 'f_db'")`
enter this command on mysql terminal
$> mysql
GRANT ALL PRIVILEGES ON f_db.* TO 'chrathan'#'%' identified by 'secret'
for add user to database in mysqlworkbench look at this page How to Create a MySQL Database with MySQL Workbench
maybe you should check db name whether is same, the same problem occour on me, finally i found mysqlclient db name is not same as i use in python