Unable to connect to Mysql with Python code on external server [Ubuntu] - python

i want to connect MySQL in external server on Ubuntu. i already have Apache server in there and i have created table.i try to connect it form Raspberry Pi with python code but it doesn't work please help me
Import part
import bluetooth
import time
import mysql.connector
import MySQLdb
import os
import datetime
Connection part
#connect to db
db = MySQLdb.connect("133.2.206.154","kgam","password","kgam" )
#setup cursor
cursor = db.cursor()
sql = "SELECT * FROM user"
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
except:
print "Error: unable to fetch data"
Error
This is my error code

Related

Is it possible to locally connect with python to an RDS instance in a VPC?

I have an RDS (Postgres) instance operational in a VPC. I have successfully configured lambda functions to connect, but I would like to be able to connect locally from my computer. Is this something that is possible? I have read about using an EC2 bastion host, but I would really like to just be able to run it without any additional architecture.
I've tried modifying this code from a different SO answer to no avail:
import psycopg2
from sshtunnel import SSHTunnelForwarder
import time
with SSHTunnelForwarder(
('database host', 22),
ssh_password="password",
ssh_username="user",
remote_bind_address=('127.0.0.1', 5432)) as server:
conn = psycopg2.connect(database="db",port=server.local_bind_port)
curs = conn.cursor()
sql = "select * from tableA limit 10;"
curs.execute(sql)
rows = curs.fetchall()
print(rows)

Connect to MySql DB using mysql.connector

I am using mysql.connector to connect to a mysql DB, while i can connect manually to the db using sql server management, if i try connecting via code, it returns this error after awhile :
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'host:1234', system error: 10054 An existing connection was forcibly closed by the remote host
These are the connection details :
connection = mysql.connector.connect(host='host',
port = '1234',
database='DBname',
user='Usr',
password='pwd')
If I create a local mysql DB, the connection works just fine.
I assume some security stuff is going on, anyone else had encountered this situation ? Anything that I'm doing wrong? Should I add anything to the connection.connect input ?
Full code for reference :
import mysql.connector
from mysql.connector import Error
connection = mysql.connector.connect(host='host',
port = '1234',
database='DBname',
user='Usr',
password='pwd')
sql_select_Query = "select * from TableName"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
print("Total numb of rows selected is : ", cursor.rowcount)
print("\nPrinting each row")
for row in records:
print(row)
connection.close()

Python mysqldb stops working after trying to connect to DB

I'm trying to connect to a external database but after trying to connect to the database, the whole script stops running. i'm trying to run it on a Raspberry PI 3. So after the attempt to connect to the database, it stops working. the test print does show up but the second one does not.
Script:
import MySQLdb
HOST = "host"
PORT = 3306
USER = "user"
PASS = "pass"
DATABASE = "databasename"
print("test")
db = MySQLdb.connect(HOST, USER, PASS, DATABASE)
print("test2")
cursor = db.cursor()
cursor.execute("SELECT * FROM Persoon")
data = cursor.fetchone()
print "database version :%s"% data
db.close()

Pymysql Windows Authentication Error

I am attempting to connect to SQL Server Management Studio. My code is below and returns the error: "init() got an unexpected keyword argument 'trusted'." I am using pymysql, would like to connect using Windows Authentication, and am running on 64 bit windows machine. Any help would be appreciated.
Edit - removed trusted. Error I am now receiving is 'No connection could be made because the target machine actively refused it'
import pymysql
import pymysql.cursors
conn = pymysql.connect(host = 'DESKTOP-6CIMC97')
Have you checked out the example on GitHub? trusted doesn't seem to be required.
https://github.com/PyMySQL/PyMySQL/blob/master/example.py
#!/usr/bin/env python
from __future__ import print_function
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
print(cur.description)
print()
for row in cur:
print(row)
cur.close()
conn.close()

Connecting to remote PostgreSQL database over SSH tunnel using Python

I have a problem with connecting to a remote database using SSH tunnel (now I'm trying with Paramiko). Here is my code:
#!/usr/bin/env python3
import psycopg2
import paramiko
import time
#ssh = paramiko.SSHClient()
#ssh.load_system_host_keys()
#ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#ssh.connect('pluton.kt.agh.edu.pl', 22, username='aburban', password='pass')
t = paramiko.Transport(('pluton.kt.agh.edu.pl', 22))
t.connect(username="aburban", password='pass')
c = paramiko.Channel(t)
conn = psycopg2.connect(database="dotest")
curs = conn.cursor()
sql = "select * from tabelka"
curs.execute(sql)
rows = curs.fetchall()
print(rows)
A problem is that the program always tries to connect to the local database. I tried with other SSH tunnels and there was the same situation. Database on remote server exists and works fine using "classical" SSH connection via terminal.
You can try using sshtunnel module that uses Paramiko and it's Python 3 compatible.
Hopefully it helps you... I scratched myself the head for a while too to do it within Python code and avoid SSH external tunnels, we need to thank developers that wrap complex libraries into simple code!
Will be simple, generate a tunnel to port 5432 in localhost of remote server from a local port then you use it to connect via localhost to the remote DB.
This will be a sample working code for your needs:
#!/usr/bin/env python3
import psycopg2
from sshtunnel import SSHTunnelForwarder
import time
with SSHTunnelForwarder(
('pluton.kt.agh.edu.pl', 22),
ssh_password="password",
ssh_username="aburban",
remote_bind_address=('127.0.0.1', 5432)) as server:
conn = psycopg2.connect(database="dotest",port=server.local_bind_port)
curs = conn.cursor()
sql = "select * from tabelka"
curs.execute(sql)
rows = curs.fetchall()
print(rows)

Categories