how to use Python3.3 access online MySQL Database - python

I am new to python and network programming.
Recently I am having trouble accessing my online database.
The problem is that I want to write a code and execute it on my computer to acces my online data base, and the only thing I want to do is to access the informaion in my database and do some change.
The first question is that I don't know what to use as parameters.
here is what show up on my online database, which I can access using internet browser:
173.201.136.195
Server version: 5.0.96-log
Server: 173.201.136.195 via TCP/IP
User: user123123#72.167.233.37
MySQL charset: UTF-8 Unicode (utf8)
I don't understand which ip should I use when i connect to it, '173.201.136.195' or '173.201.136.195' and which username should I use. 'user123123' or 'user123123#72.167.233.37'
This is the code I use:
conn = pymysql.connect(host='173.201.136.195',user='user123123',passwd="123123123",db='TestData')
phthon shell shows that:
File "F:\Program Files\Python33\lib\pymysql\connections.py", line 819, in _connect 2003, "Can't connect to MySQL server on %r (%s)" % (self.host, e))
pymysql.err.OperationalError: (2003, 'Can\'t connect to MySQL server on \'173.201.136.195\' ((1045, "Access denied for user \'user123123\'#\'142.151.201.116\' (using password: YES)"))')
What on earth is 142.151.201.116 ?

the error means that access to the server 173.201.136.195 is denied to the user user123123 at ip 142.151.201.116. When you give access to a user on mysql database you specify the ip or wildcads for multiple ips. So access to the database for user is per ip.
example:
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
will give access to username on all hosts. but this:
GRANT ALL PRIVILEGES ON *.* TO 'username'#'10.20.30.40' IDENTIFIED BY 'password' WITH GRANT OPTION;
will give access to username at ip 10.20.30.40
this information is on mysql.user table

Related

SQLalchemy mysql url connection string on cloud sql

What's the conncetion URL for mysql in sqlalchemy while connecting to google app engine ??
I know it's
mysql+gaerdbms:///<dbname>?instance=<my cloud instance name>
But where to specify user, password and host. I get below error while connecting with above string.
947, in MakeRequest raise _ToDbApiException(response.sql_exception) OperationalError: (OperationalError) (1045, u"Access denied for user 'root'#'localhost' (using password: NO)") None None
The mysql+gaerdbms:// is deprecated. Please use the mysql+mysqldb:// instead. The format of the connection URI is:
mysql+mysqldb://root#/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>
Note that the above only works in App Engine prod. The recommended way to connect from dev_appserver is to request an IP and use that to connect.
Reference: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html?highlight=appengine#module-sqlalchemy.dialects.mysql.gaerdbms

Query MySQL db from Python returns "Access Denied"

I am using
db = MySQLdb.connect(host="machine01", user=local_settings.DB_USERNAME, passwd=local_settings.DB_PASSWORD, db=local_settings.DB_NAME)
to connect to a DB, but I am doing this from machine02 and I thought this would still work, but it does not. I get
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'test_user'#'machine02' (using password: YES)")
as a result. However, if I simply ssh over to machine01 and perform the same query, it works just fine. Isn't the point of host to be able to specify where the MySQL db is and be able to query it from any other host instead of having to jump on there to make the query?
Make sure your firewall isn't blocking port 3306.
The error tells you that 'test_user' at machine 'machine02' is not allowed. Probably user 'test_user' is on 'mysql.user' table registered with 'localhost' as connection's host. Check it using a query like this: select host, user from mysql.user;
Best regards,
Oscar.

Python scripts and MySQL

Good day!
I have a question annoying me. I setup Apache server/MySQL/PhpMyAdmin on my PC. And i'd like to write some scrips on python3 to work with database. I setup my db on localhost. But now the local network has another one computer.
I wish my friend could access the database through my ip. like (http :// 192.168.xx.xx / pma).
and now it works!
My friend loves python too. And there is a problem: When he's run script, python waits 30 sec and:
"Can't connect to MySQL server on %r (%s)" % (self.host, e))
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '192.168.
xx.xx' ([WinError 10060] A connection attempt failed because the connected part
y did not properly respond after a period of time, or established connection fai
led because connected host has failed to respond)")
There peace of code to connect:
import pymysql
con = pymysql.connect(host='192.168.xx.xx', user='root', passwd='xxx', db='test')
cur = con.cursor()
print(con)
Help my friend to work with my database!
Thanks you!
You have to give remote access to your mysql database.
Then give grant to your user.
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

Connecting to MS-SQL from pyodbc using windows authentication

I am trying to connect to MSSQL server using pyodbc. I can connect to the server and query it using the basic authentication mode as:
connection = pyodbc.connect("DRIVER={Easysoft ODBC-SQL Server};SERVER=192.168.2.119;DATABASE=dbame;UID=**;PWD=****")
Connection to MSSQL can also be done using Windows Authentication where it takes the parameters
DOMAIN
USERNAME
PASSWORD
I don't know how to use this sort of credential from pyodbc to connect to the MSSQL Server.
Furthermore, the ODBC driver I am using (Easysoft ODBC-SQL Server) needs licensing. Don't we get such drivers for free?
The Easysoft SQL Server driver parameters to use NTLM authentication are
Trusted_Domain=<domain name>
NTLMv2=Yes|No
Trusted_Connection=Yes|No
And UID, PWD as usual.
NTLM can also be triggered simply using a UID that looks like
DOMAIN\USER
If you want to use Kerberous, the following can be set
ServerSPN=SPN
Its all the the user guide for the driver
connection = pyodbc.connect("DRIVER={Easysoft ODBC-SQL Server};SERVER=192.168.2.119;DATABASE=dbame;UID=;PWD=**")
The string part of the connection is what is known as a DSN-Less connection so you can pass in any of the attributes required for example :-
connection = pyodbc.connect("DRIVER={Easysoft ODBC-SQL Server};SERVER=192.168.2.119;DATABASE=dbame;UID=MyWindowsUserName;PWD=MyPassword;Trusted_Domain=MyWindowsDomainName;Trusted_Connection=1")
Trusted_Connection = 1 tells the Easysoft driver that you intend to use the Trusted_Domain, user ( UID ) and password ( PWD ) to login to SQL Server.
For a full list of all the attributes available within the Easysoft ODBC-SQL Server Driver please read the Attributes section of the manual.

How to connect to remote mysql server in python

Hi i have a requirement where i need to connect to remote mysql server. My application shall be running on local machine and my mysql will be running on remote server.I have tried the following code:
DB = 'gts'
DB_HOST = 'ps95074.dreamhost.com'
DB_USER = 'root'
DB_PASSWORD = 'dbadminpassword'
conn = MySQLdb.Connection(db=DB, host=DB_HOST, user=DB_USER,passwd=DB_PASSWORD)
cursor = conn.cursor()
But i am getting the following error
OperationalError: (2005, "Unknown MySQL server host 'ps95074.dreamhost.com' (1)")
Instead if i use
DB_HOST='localhost'
Everything works fine. How can same be possible with remote host.Any help shall be appreciated.
Check your firewall. That server is online and available from any machines:
> mysql -h ps95074.dreamhost.com
ERROR 1045 (28000): Access denied for user 'myuser'#'myhost' (using password: NO)
However, even if you can connect chances are good that your database user only allows local connections.
Update: I just tried it again and now it also fails using the commandline client. So clearly something is wrong with your server.

Categories

Resources