I am trying to query to my database running on AWS over an SSH tunnel with Pandas. But I fail login. Also there is one key that I use to connect on SQL client (Sequel Pro) that I don't know how to use in my python code.
This is the key: heroes-us-west-1-master.cheme3bzdejb.us-west-1.rds.amazonaws.com
This is my code:
with SSHTunnelForwarder(
(host, 22),
ssh_username=ssh_username,
ssh_private_key=ssh_private_key,
remote_bind_address=(localhost, 3306)
) as server:
conn = db.connect(host=localhost,
port=server.local_bind_port,
user=user,
passwd=password,
db=database)
return pd.read_sql_query(q, conn)
df = query("SELECT stores.name, stores.address\
FROM stores;")
This is the error message:
Traceback (most recent call last):
File "connection.py", line 32, in <module>
FROM stores;")
File "connection.py", line 22, in query
remote_bind_address=(localhost, 3306)
File "/Users/Solal/Desktop/SQL/venv/lib/python3.6/site-packages/sshtunnel.py", line 904, in __init__
logger=self.logger
File "/Users/Solal/Desktop/SQL/venv/lib/python3.6/site-packages/sshtunnel.py", line 1095, in _consolidate_auth
raise ValueError('No password or public key available!')
ValueError: No password or public key available!
Related
I'm using peewee for a Python ORM to a MySQL database and Flask to serve a web site from my local desktop running Ubuntu. I can connect to the database from the command line (mysql test -u user -p), but am getting a connection refused error when trying from Python. This code has worked on my previous Ubuntu PC, and I dumped the database from there, imported into the new PC and granted permissions, so I'm really not sure why it's giving me this.
Model.py:
from peewee import MySQLDatabase, Model, AutoField, CharField, IntegerField, \
ForeignKeyField, BooleanField, TextField, DateTimeField
from flask_security import UserMixin, RoleMixin
class BaseModel(Model):
"""
Base model for the model classes; prevents having to define Meta class with the
database for each Model class. Must call BaseModel._meta.database.init() before
connecting - should be able to just call once on BaseModel before
using derived model classes.
"""
class Meta:
database = MySQLDatabase(None) #init before calling connect()
#Additional code to define various tables
Trying to connect in Python command line:
import Model as mmm
db = mmm.BaseModel._meta.database
db.init('test', host = 'localhost', user = 'mike', passwd = password, port = 3306)
db.connect()
Error stack trace:
Traceback (most recent call last):
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 569, in connect
sock = socket.create_connection(
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3035, in connect
self._state.set_connection(self._connect())
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3933, in _connect
conn = mysql.connect(db=self.database, **self.connect_params)
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 619, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'aspire' ([Errno 111] Connection refused)")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3038, in connect
self._initialize_connection(self._state.conn)
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 2873, in __exit__
reraise(new_type, new_type(exc_value, *exc_args), traceback)
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 183, in reraise
raise value.with_traceback(tb)
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3035, in connect
self._state.set_connection(self._connect())
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3933, in _connect
conn = mysql.connect(db=self.database, **self.connect_params)
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 619, in connect
raise exc
peewee.OperationalError: (2003, "Can't connect to MySQL server on 'aspire' ([Errno 111] Connection refused)")
I am running a discord bot using a MySQL server which are BOTH running on my UBUNTU 18.04 server. It works fine, but after a few hours I start to get an error any time I access the database.
Traceback (most recent call last):
File "/home/narnar/.local/lib/python3.7/site-packages/discord_slash/client.py", line 1352, in invoke_command
await func.invoke(ctx, **args)
File "/home/narnar/.local/lib/python3.7/site-packages/discord_slash/model.py", line 209, in invoke
return await self.func(self.cog, *args, **kwargs)
File "/home/narnar/cool-art/cogs/artlevels.py", line 120, in leaderboardCommand
cur.execute("SELECT * FROM artLevels")
File "/home/narnar/.local/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 271, in execute
raw_as_string=self._raw_as_string)
File "/home/narnar/.local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 522, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.OperationalError: 2013 (HY000): Lost connection to MySQL server during query
I solved this issue by not having connections open for too long. My program connected to the MySQL server at the beginning of the program and used that same connection all the time. So instead of having one connection, I just opened a connection when I needed to access the database then closed it afterwards. Just make sure you don't have your connection open for too long
Don't do this:
con = mysql.connector.connect(
host=host,
user="ImNotThat",
passwd="Stupid",
database="toShowMyPasswords"
)
async def someListenerEvent():
doThing()
con.commit()
Do this:
async def someListenerEvent():
con = mysql.connector.connect(
host=host,
user="ImNotThat",
passwd="Stupid",
database="toShowMyPasswords"
)
doThing()
con.commit()
cur.close()
con.close()
MySQL is not connecting and show error
I install mysql.connector package through pip
This is Code
import mysql.connector
connection = mysql.connector.connect(host = "localhost", user = "root",passwd = "268455")
print(connection)
This is Error im getting
D:\Python>"C:/Users/Abdul Muqeet/AppData/Local/Programs/Python/Python38/python.exe"
"d:/Python/exmaple (mySQL).py"
Traceback (most recent call last):
File "d:/Python/exmaple (mySQL).py", line 2, in <module>
connection = mysql.connector.connect(host = "localhost", user = "root",passwd = "268455")
File "C:\Users\Abdul Muqeet\AppData\Roaming\Python\Python38\site-
packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\Abdul Muqeet\AppData\Roaming\Python\Python38\site-
packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Users\Abdul Muqeet\AppData\Roaming\Python\Python38\site-
packages\mysql\connector\abstracts.py", line 716, in connect
self._open_connection()
File "C:\Users\Abdul Muqeet\AppData\Roaming\Python\Python38\site-
packages\mysql\connector\connection.py", line 208, in _open_connection
self._do_auth(self._user, self._password,
File "C:\Users\Abdul Muqeet\AppData\Roaming\Python\Python38\site-
packages\mysql\connector\connection.py", line 137, in _do_auth
packet = self._protocol.make_auth(
File "C:\Users\Abdul Muqeet\AppData\Roaming\Python\Python38\site-
packages\mysql\connector\protocol.py", line 99, in make_auth
packet += self._auth_response(client_flags, username, password,
File "C:\Users\Abdul Muqeet\AppData\Roaming\Python\Python38\site-
packages\mysql\connector\protocol.py", line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
File "C:\Users\Abdul Muqeet\AppData\Roaming\Python\Python38\site-
packages\mysql\connector\authentication.py", line 190, in get_auth_plugin
raise errors.NotSupportedError(
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not
supported
I search many sites,i did in same way all did but still getting error
You have to include the database that you want to connect with it and the message can not be like you use.
A example:
def MySQLConnect(usernameStr, passwordStr, hostStr, databaseStr, conPrint = True):
#ESTABLISH THE CONNECTION WITH THE DATABASE
try:
con = mysql.connector.connect(
user = usernameStr,
password = passwordStr,
host = hostStr,
database = databaseStr)
if conPrint: print('Connection established successfully with the database')
return con
except Error:
print(Error)
Issue is solved. I install mysql-connector package while I need to install mysql-connector-python package. First uninstall mysql-connector package and then install mysql-connector-python package.
I'm trying to use sshtunnel and MySQLdb in python2.7 to tunnel my connection and facing following problem:
I used Sequel Pro to connect mysql was OK, but the python code did not work!
Sequel Pro is like this:
connect info
and code is like this:
`from sshtunnel import SSHTunnelForwarder
import MySQLdb
with SSHTunnelForwarder(
('2.2.2.2', 22),
ssh_username='name2',
ssh_password='mypassword',
remote_bind_address=('127.0.0.1', 3306)
) as tunnel:
connection = MySQLdb.connect(
user='name1',
password='mypassword',
host='1.1.1.1',
database='mydata',
port=3306)
I searched some example code like:
from sshtunnel import SSHTunnelForwarder
import MySQLdb
with SSHTunnelForwarder(
(_host, _ssh_port),
ssh_username=_username,
ssh_password=_password,
remote_bind_address=(_remote_bind_address, _remote_mysql_port),
local_bind_address=(_local_bind_address, _local_mysql_port)
) as tunnel:
connection = MySQLdb.connect(
user=_db_user,
password=_db_password,
host=_local_bind_address,
database=_db_name,
port=_local_mysql_port)
I want to know that did I do the right way to built ssh or connect to mysql? Thanks for help!
update:
the error message:
2017-06-15 17:52:58,415| ERROR | Could not connect to gateway 1.1.1.1:22 : 110
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1483, in __enter__
File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1225, in start
File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1037, in _raise
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway
Update your code to add the local_bind_address:
from sshtunnel import SSHTunnelForwarder
import MySQLdb
with SSHTunnelForwarder(
('2.2.2.2', 22),
ssh_username='name2',
ssh_password='mypassword',
remote_bind_address=('127.0.0.1', 3306),
local_bind_address = ('1.1.1.1', 3306)
) as tunnel:
connection = MySQLdb.connect(
user='name1',
password='mypassword',
host='1.1.1.1',
database='mydata',
port=3306)
Unless you specify your Localhost (as '1.1.1.1' in your case) & Port; MYSQL service will not be available for the localhost.
I Hope this helps.
On TurnkeyLinux I have tried to connect a Web2Py app to the MySQL instance (which is up and running) with the following DAL statement (in db.py):
db = DAL('mysql://root:pwd2sql#web2py/dbname', fake_migrate_all=True)
It doesn't work and throws the following ticket:
Failure to connect, tried 5 times: Traceback (most recent call last): File "/var/www/web2py/gluon/dal.py", line 7562, in init self.adapter = ADAPTERSself._dbname File "/var/www/web2py/gluon/dal.py", line 2572, in __init_ if do_connect: self.reconnect() File "/var/www/web2py/gluon/dal.py", line 606, in reconnect self.connection = f() File "/var/www/web2py/gluon/dal.py", line 2570, in connector return self.driver.connect(*driver_args) 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 187, in init super(Connection, self).init(args, **kwargs2) OperationalError: (2003, "Can't connect to MySQL server on 'web2py' (111)")
Does anybody have a connection string sample I can use ?