Python SQL connect to wrong ip adress - python

I'm trying to connect to my sql server using a simple python script.
import MySQLdb
db = MySQLdb.connect(host="192.168.0.156", user="root",
passwd="Imnottellingyoumypassword", db="mydatabase") # name of the data base
cur = db.cursor()
cur.execute("SELECT * FROM customers")
for row in cur.fetchall() :
print row[0]
But when I run my script something goes wrong:
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)
_mysql_exceptions.OperationalError: (1045, "Access denied for user
'root'#'192.168.0.107' (using password: YES)")
Why is he trying to connect to my ip address of my laptop instead of the sql server?

That message is from the MySQL server and not the MySQL driver. What the server is telling you is that connections as root from the IP of your laptop aren't allowed.

Related

pymysql.err.InternalError: (1049, "Unknown database")

I want to connect MySQL RDS DB using python from raspberrypi.(i want to get seq from MySQL table 'face' using select query.)
and I have an error but i can not fix it.
This is rds mysql connection code:
import rds_config
import pymysql
rds_host = rds_config.rds_host
name = rds_config.rds_user
password = rds_config.rds_pwd
db_name = rds_config.rds_db
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name,
connect_timeout=10)
with conn.cursor() as cur:
cur.execute("select seq from face")
conn.commit()
rds_config:
rds_host='rds endpoint'
rds_port=3306
rds_user='user'
rds_pwd='password'
rds_db='db name'
and This is traceback:
Traceback (most recent call last):
File "getRds.py", line 18, in <module>
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=10)
File "/usr/local/lib/python2.7/dist-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 598, in connect
self._request_authentication()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 862, in _request_authentication
auth_packet = self._process_auth(plugin_name, auth_packet)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 933, in _process_auth
pkt = self._read_packet()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 683, in _read_packet
packet.check_error()
File "/usr/local/lib/python2.7/dist-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python2.7/dist-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1049, u"Unknown database 'bsb-rds'")
i alread added ip address in vpc security group and public access is on.
it was possible to connect through mysql cli or workbench.
can anyone help me?
tl;dr you need to create bsb-rd. Execute this command: create database bsb-rds either with cur.execute() in python or in your favorite sql cli.
If you get this error, good news! You can connect to your RDS instance. This means you have the security group set up right, the host url, username, password and port are all correct! What this error is telling you is that your database bsb-rds does not exist on your RDS instance. If you have just made the RDS instance it is probably because you have not created the database yet. So create it!
Here are two ways to do this
mysql cli
In your terminal run
mysql --host=the_host_address user=your_user_name --password=your_password
Then inside the mysql shell execute
create database bsb-rd;
Now try your code again!
Python with pymysql
import rds_config
conn = pymysql.connect('hostname', user='username', passwd='password', connect_timeout=10)
with conn.cursor() as cur:
cur.execute('create database bsb-rd;')
I came to this page looking for a solution and didn't get it. I eventually found the answer and now share what helped me.
I ran into your exact issue and I believe I have the solution:
Context:
My tech stack looks like the following
Using AWS RDS (MySQL)
Using Flask Development on local host
RDS instance name: "test-rds"
Actual DB Name: test-database
Here is where my issue was and I believe your issue is in the same place:
You are using the AWS RDS NAME in your connection rather than using the true Database name.
Simply changing the DB name in my connection to the true DB name that I had setup via MySQL Workbench fixed the issue.
Other things things to note for readers:
Please ensure the following:
If you are connecting from outside your AWS VPC make sure you have public access enabled. This is a huge "gotcha". (Beware security risks)
Make sure your connection isn't being blocked by a NACL
Make sure your connection is allowed by a Security Group Rule

PyMySQL keeps connection error

I've tried a bunch of thinks including trying to specify the UNIX socket to no avail, I'm not running any queries and I haven't even initialized a cursor but I keep getting this error, what gives?
Python Block:
connection = mysql.connect(user = "root", password = None, port = 8080, host = 'localhost', db ='Articles')
Error:
Traceback (most recent call last):
File "/Users/Adrian/Desktop/Python/webcrawl.py", line 10, in <module>
connection = mysql.connect(user = "root", password = None, port = 8080, host = 'localhost', db ='Articles')
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 699, in __init__
self.connect()
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 935, in connect
self._get_server_information()
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 1249, in _get_server_information
packet = self._read_packet()
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 991, in _read_packet
packet_header = self._read_bytes(4)
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 1037, in _read_bytes
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
Edit: I'm running XAMPP 7.2 on MacOSX with port forwarding enabled over SSH (localhost:8080 -> 80) and the opt/lampp volumes are mounted
It's not advisable to use root without password.
In some databases you are forced to set it if you want to connect as root.
You can set any password: dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
Also, 8080 might be the port of your web server, not the mySQL one, try with 3306 port which is the default one for mySQL.
You can open my.cnf file located in the /Applications/XAMPP/xamppfiles/etc/ directory to chech in which port is your database listening.
Also check that the mySQL daemon is running. In mac os X go to /Applications/XAMPP/XAMPP Control in Finder and check that Apache and MySQL are running.
If your MySQL server isn't starting, you may need to set the permissions for it using Terminal with this command:
chmod -R 777 /Applications/XAMPP/xamppfiles/var
To check that your Articles database exists:
mysql -u root -p
USE Articles;
If it's not created;
mysql -u root -p
CREATE DATABASE Articles;
And connect this way:
#!/usr/bin/python
import MySQLdb
connection = MySQLdb.connect(host="localhost",
user="root",
passwd="your pwd",
db="Articles")

Python connect to 000webhost web server MySQL database

I am trying to connect Python to a web server's database, 000webhost I am using. Here is my code:
import MySQLdb
>>> conn = MySQLdb.connect(host="mysql9.000webhost.com",user="user_name",passwd="password",db="db_name")
I think I install MySQLdb successfully. And all the username, password, host, database name are exactly the same as they are in my php file, which works perfectly. However, I got this error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
conn = MySQLdb.connect(host="mysql9.000webhost.com",user="a6969519_123",passwd="lianshiyu08",db="a6969519_shiyu")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'mysql9.000webhost.com' (60)")
What did I miss?
The database is only reachable from the internal web of your hoster.

Peewee - Can't connect to MySQL server on host

I'm developing a Flask based python app using the peewee ORM. I was initially connecting to the database that was being stored locally on my machine and I'm now trying to transition to connecting to the db remotely. I've set up the database in phpmyadmin via my server's cpanel section.
The Issue
I've set up my IP address to be able to remotely access my databases but I am getting the following error when I attempt to connect to the database:
Traceback (most recent call last):
File "app.py", line 294, in <module>
models.initialize()
File "/Users/wyssuser/Desktop/dscraper/models.py", line 145, in initialize
DATABASE.connect()
File "/Library/Python/2.7/site-packages/peewee.py", line 2767, in connect
self.__local.closed = False
File "/Library/Python/2.7/site-packages/peewee.py", line 2688, in __exit__
reraise(new_type, new_type(*exc_value.args), traceback)
File "/Library/Python/2.7/site-packages/peewee.py", line 2766, in connect
**self.connect_kwargs)
File "/Library/Python/2.7/site-packages/peewee.py", line 3209, in _connect
return mysql.connect(db=database, **conn_kwargs)
File "/Library/Python/2.7/site-packages/pymysql/__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/pymysql/connections.py", line 644, in __init__
self._connect()
File "/Library/Python/2.7/site-packages/pymysql/connections.py", line 869, in _connect
raise exc
peewee.OperationalError: (2003, "Can't connect to MySQL server on '142.157.25.22' ([Errno 61] Connection refused)")
This is the portion of my code that references the database connection:
app.py
if __name__ == '__main__':
models.initialize()
app.run(debug=DEBUG, port=PORT, host=HOST)
config.py
DATABASE = {
'db': 'my_dbname',
'host': '142.157.25.22',
'port': 3306,
'user': 'my_username',
'passwd': 'my_pswd',
}
models.py
from peewee import *
import config
DATABASE = MySQLDatabase(config.DATABASE['db'], host=config.DATABASE['host'], port=config.DATABASE['port'], user=config.DATABASE['user'], passwd=config.DATABASE['passwd'])
...all of my models related code
def initialize():
print 'starting db connection'
DATABASE.connect()
print 'connected'
DATABASE.create_tables([Batch, Company, User, Post],safe=True)
DATABASE.close()
I've also tried connecting to 'localhost' as the host but that doesn't seem to work here, is there a different host I should be connecting to?
Solution is bad default port:
the example from peewee doc is
# Connect to a MySQL database on network.
mysql_db = MySQLDatabase('my_app', user='app', password='db_password',
host='10.1.0.8', port=3316)
but defaul port is 3306

Python database creation - MySQL error

I have been trying to create a database in python using the MySQL module. Here's the code:
import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1', user='root', passwd='', db='test', port='3306')
cursor = conn.cursor()
cursor.execute("SELECT VERSION()")
row = cursor.fetchone()
print "server version:",row[0]
cursor.close()
conn.close()
I had errors in initial steps relating to the host name, which I changed from localhost to the local IP and added a port keyword which seems to work with the quotes only. Apart from this, I am encountering the following error:
Traceback (most recent call last):
File "C:\Python27\server_version", line 4, in <module>
conn = MySQLdb.connect(host = '127.0.0.1', user='root', passwd='', db='bhavin', port='3306')
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required
port should be an integer, not a string:
conn = MySQLdb.connect(host='127.0.0.1',
user='root',
passwd='',
db='test',
port=3306)
Though, note that 3306 is a default port used if not provided explicitly:
port
TCP port of MySQL server. Default: standard port (3306).
Any way you are not creating a db table with that code, you are going to get an error, like "No such table exists" or something like that. Your should try: CREATE TABLE IF NOT EXISTS(" // columns of your table with the type like integer or varchar

Categories

Resources