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!!
Related
I am beginning with sqlite3 in Python.
I know how to connect to local database. See simple example below:
import sqlite3
connection = sqlite3.connect('mydb.db')
cursor = connection.cursor()
But now I would like to send data into a database that is running on a server. I have following config information:
[db-my_db]
user=my_user
password=my_pass
host=db.my_db.com
database=my_db.com
How can I connect to this database? Thanks
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 am trying to connect to my GearHost Database in python, I followed the instructions here on GearHost. My python code looks like:
from mysql.connector import connection
server = 'den1.mssql6.gear.host'
db = 'testmenow'
user = 'testmenow'
psword = 'TEST!!'
cnxn = connection.MySQLConnection(host=server, user=usr, password=psword, database=db)
cursor = cnxn.cursor()
cnxn.close()
I get the following error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'den1.mssql6.gear.host:3306' (10061 No connection could be made because the target machine actively refused it)
I have also tried to connect to GearHost through mysql workbench, as instructed in GearHost's instruction page, which also cannot connect to the database:
Connecting to MySQL server ...
Can't connect to MySQL server on 'den1.mssql6.gear.host' (10060)
This is what my GearHost shows:
Tried (unsuccessfully)
Messing with and without a port number, as mentioned here: Issue connecting to gearhost database
Trying connecting through MySQL workbench
Considering the conversation here: Cannot Connect to Database Server mysql workbench , but does not seem applicable to my circumstances.
Different SQL packages in python, including mysql-python and mysql-connector-python
Question
Am I missing something obvious that is preventing me from connecting to GearHost, either with python or mysql workbench?
UPDATE
as #SMor pointed out, I had mistaken MSSQL for MySQL - I was able to successfully connect to my database using:
import pyodbc
server = 'den1.mssql6.gear.host'
db = 'testmenow'
usr = 'testmenow'
psword = 'TEST!!'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';'
'DATABASE='+db+';UID='+usr+';PWD=' + psword)
cursor = cnxn.cursor()
I have tried the following code:
MySQLdb.connect(host='xxx', port=3306, user='yyy')
But I get the following error:
(2005, "Unknown MySQL server host ...
I have tried to remove all firewall restrictions on the external MySQL instance, as a test. I am able to connect to the instance from my developing machine.
I believe this should be possible now that the App Engine supports sockets, or am I wrong?
I think this connection is not allowed (no external socket support in MySQLdb) :
https://developers.google.com/appengine/docs/python/cloud-sql/?hl=en#Python_Using_a_local_MySQL_instance_during_development
You have to use localhost/127.0.0.1 or CloudSQL socket (unix_socket='/cloudsql/'):
if (os.getenv('SERVER_SOFTWARE') and
os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='guestbook', user='root')
else:
db = MySQLdb.connect(host='127.0.0.1', port=3306, user='root')
# Alternately, connect to a Google Cloud SQL instance using:
# db = MySQLdb.connect(host='ip-address-of-google-cloud-sql-instance', port=3306, user='root')
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()