I am trying to use python to get a connection to MySQL,
It is python code is :
import MySQLdb
conn= MySQLdb.connect(
host='public ip',
port = 3306,
user='root',
passwd='123456',
db ='test_schema',
)
so it always give the errors:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'pubilic ip' (10060)")
Building off AK47's answer
You need to first find the public facing ip address of your machine.
Go to google and search What's my ip address you should get a set of numbers xx.xx.xxxx.xx for example 12.42.111.2
Then in your mysql you need to modify the conf file if linux, ini file if windows
/etc/mysql/my.cnf Unix/OSX systems.
C:\Program Files\MySQL\MySQL Server 5.5\ Windows system
change bind-address to your ip address from google.
Then in AK47's answer replace host='127.0.0.1' to host=<ip from google>
Update your connection parameters to have an actual IP address in the 'host' field
import MySQLdb
conn= MySQLdb.connect(
host='127.0.0.1',
port = 3306,
user='root',
passwd='123456',
db ='test_schema')
Related
I am trying to connect to GHTorrent database through ssh in python so I can deal with the data.
There is the example of how this ssh works in command lines and it works.
http://ghtorrent.org/mysql.html
import pymysql
from sshtunnel import SSHTunnelForwarder
mypkey = paramiko.RSAKey.from_private_key_file("/Users/***/.ssh/id_rsa")
with SSHTunnelForwarder(
('web.ghtorrent.org', 3306),
ssh_username="ghtorrent",
ssh_pkey=mypkey,
ssh_private_key_password="*****",#my password for my pc
remote_bind_address=('web.ghtorrent.org', 3306)) as server:
conn = pymysql.connect(host='127.0.0.1',
port=server.local_bind_port,
user='ght',
passwd='',
db='ghtorrent')
From my code the ssh can't connect to the server. I am not really sure whether my connection information is correct.
Since it uses a website name rather than the IP address so I have no idea whether it works.
Thank you so much!
Firstly, the GHTorrent SSH server is listening on port 22, you are trying to connect to 3306, which is incorrect.
Also, have you tried just specifying the SSH private key path directly?
i.e. ssh_pkey="/Users/***/.ssh/id_rsa"
SSHTunnelForwarder(
('web.ghtorrent.org', 22),
ssh_username="ghtorrent",
ssh_pkey="/Users/***/.ssh/id_rsa",
ssh_private_key_password="*****",#my password for my pc
remote_bind_address=('web.ghtorrent.org', 3306))
pyodbc doesn't seem to be able to connect when trying to connect through specifying Driver. I am able to connect to setting up a DSN but I also want to make connection when user has got the Driver, Server, UID, PWD and Database details.
I am on Mac and and using FreeTDS driver.
freetds.conf
[MYMSSQL]
host = 0.0.0.0
port = 1433
tds version = 7.3
odbcinst.ini
[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=10
Here is how I am trying to connect:
conn_str = "DRIVER=FreeTDS;SERVER={0};UID={1};PWD={2};DATABASE={3}".format('MYMSSQL', 'sa', 'password','tempdb')
conn = pyodbc.connect(conn_str)
The error I get is this:
pyodbc.OperationalError: ('08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
Exact same database details work when I try to connect through DSN.
If you have a freetds.conf file containing the host name/IP and port, e.g.,
gord#xubuntu64-nbk1:~$ cat /etc/freetds/freetds.conf
[myFreeTdsHost]
host = 192.168.0.179
port = 49242
then you can use both of those values in your DSN-less connection string by simply specifying the SERVERNAME=
# get host name/IP and port from freetds.conf
cnxn_str = (
'DRIVER=FreeTDS_ODBC;'
'SERVERNAME=myFreeTdsHost;'
'DATABASE=myDb;'
'UID=sa;PWD=_whatever_;'
)
You can also supply the host name/IP and port directly via SERVER= and PORT= like so
# supply host name/IP and port directly (bypassing freetds.conf)
cnxn_str = (
'DRIVER=FreeTDS_ODBC;'
'SERVER=192.168.0.179;'
'PORT=49242;'
'DATABASE=myDb;'
'UID=sa;PWD=_whatever_;'
)
For details, see the FreeTDS documentation here.
I have tried solutions found on other SO questions but none of them have worked for me. I am attempting to pull data from a mysql db running on a remote server by setting up an ssh tunnel. My code is as follows:
server = sshtunnel.SSHTunnelForwarder(
('10.6.41.10', 22),
ssh_username= 'serveruser',
ssh_password= 'serverpw',
remote_bind_address=('127.0.0.1', 3306))
server.start()
print(server.local_bind_port)
cnx = mysql.connector.connect(user='root', password='mysqlpw',
host='127.0.0.1',
database='mydb',
charset='utf8',
use_unicode='FALSE',
port = 3306)
However, when I run this code I receive:
1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I have also tried adding
local_bind_address = ('0.0.0.0', 3306)
to the sshtunnel setup and instead recieved
Problem setting SSH Forwarder up: Couldn't open tunnel 0.0.0.0:3306 <> 127.0.0.1:3306 might be in use or destination not reachable
I don't fully understand the remote_bind_address and local_bind_address, so my guess is that must be doing something wrong there. I know my username/pw/server info is correct, I am able to ssh into my server via terminal and then use
mysql -h 127.0.0.1 -u root -p
to successfully log into my mysql server. So what do I need to fix to get it running in python? Thanks.
If you don't specify local_bind_address in sshtunnel.SSHTunnelForwarder, the local port is allocated randomly. In that case set port=server.local_bind_port in mysql.connector.connect().
Instead, you can also set local_bind_address=('0.0.0.0', [some port which is not in use]) in sshtunnel.SSHTunnelForwarder. The sshtunnel.HandlerSSHTunnelForwarderError ("Problem setting...") tells you that you can't use local_bind_address=('0.0.0.0', 3306).
I had installed mysql and on python shell import MySQLdb does work. I also changed the settings.py as:
**DATABASE_ENGINE = 'mysql'
DATABASE_NAME = '/home/database/my_db.db'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''**
but when I test for the connection it fails.
terminal:
>>> from django.db import connection
>>> cursor = connection.cursor()
It gives the error as :
OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysql' (2)")
Django say its engine should be https://docs.djangoproject.com/en/dev/ref/settings/#engine 'django.db.backends.mysql'
I think database name is the name of the database inside mysql not a path. A path is just for sqlite. Do you have mysql server running?
USER, PASSWORD are required. host defaults to localhost port defualts to 3306.
Are you running a MySQL server? is it on:
localhost (unix domain socket), or
localhost (tcp), or
localhost but not accessible, or
some other host?
also, database name is name as mysql understands it, not a file path.
The location for mysql.sock on my system is /usr/local/mysql5/mysqld.sock
thrilllap-2:tmp reuven$ mysqld --print-defaults
mysqld would have been started with the following arguments:
--socket=/usr/local/mysql5/mysqld.sock --port=3306
When I try to use mysql via sqlalchemy from flask, I get:
File "build/bdist.macosx-10.6-intel/egg/MySQLdb/connections.py", line 187, in __init__
sqlalchemy.exc.OperationalError: (OperationalError) (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)") None None
The mysql program connects correctly to the database, as does every other mysql client on my system.
My my.cnf has the correct location for the socket
[client]
port = 3306
socket = /usr/local/mysql5/mysqld.sock
[safe_mysqld]
socket = /usr/local/mysql5/mysqld.sock
[mysqld_safe]
socket = /usr/local/mysql5/mysqld.sock
[mysqld]
socket = /usr/local/mysql5/mysqld.sock
port = 3306
The base "SQLAlchemy" library has an option where you can specify the location of the mysql.sock, but this isn't exposed through the sqlalchemy / flask library
http://packages.python.org/Flask-SQLAlchemy/config.html
My questions:
Where does sqlalchemy get the idea that /tmp/mysql.sock is the correct location?
Is there a way to change the default via the Flash-SQLAlchemy connector
You'll have to dig up the exact syntax, but for MySQL I think they use a unix_socket query opt. Something like:
mysql:///dbname?unix_socket=/opt/mysql/mysql.sock'
Should be your connect URI for SQLAlchemy.
Yes! Sean was right
app.config['SQLALCHEMY_DATABASE_URI'] = ''mysql://dayenu:secret.word#localhost/dayenu?unix_socket=/usr/local/mysql5/mysqld.sock
db = SQLAlchemy(app)
works fine! I think this parameter is used by pyodbc, which is what SQLAlchemy uses to talk to mysql, but I couldn't find this parameter anywhere in the pyodbc documentation.
You could create the conexión using
sqlalchemy.create_engine(
mysql_str = sqlalchemy.engine.url.URL(
drivername='mysql+pymysql',
username="db_user",
password="db_pass",
database=db_name,
query={
'unix_socket': '/usr/local/mysql5/mysqld.sock'
}
)
)