"Unknown MySQL server host" when using Flask in Python
Question:
I am trying to run a web app with Flask. I have MySQL server on my device and have changed its bind address
to 192.168.0.102
.
Now, in Python I am attempting to connect with MySQLdb:
conn = MySQLdb.connect("user='myuser', password='mypassword', host='192.168.0.102', database='usersdb'")
…with Flask app running on 192.168.0.102
app.run(debug=True, host='192.168.0.102')
Now, I get this error :
OperationalError: (2005, "Unknown MySQL server host 'user='myuser', password='mypassword', host='192.168.0.102', database='usersdb'' (0)")
I don’t know if its because MySQL is not running on 192.168.0.102
or if it is a problem with Flask.
What could be the problem?
Answers:
Try this:
Enter the IP address where the MySQL server is at in MySQLdb.connect:
conn = MySQLdb.connect(host="localhost", # MySQL host, usually localhost
user="myuser",
passwd="mypassword",
db="usersdb")
And use the address 192.168.0.102
for:
app.run(debug=True, host='192.168.0.102')
Sadly, you try to put a string
which is invalid host
to MySQLdb.connection
, it’s no the right params which function expect! you should read more documentations or try to use help function to deep understand this function. and What params its require and which type of param its expected.
import MySQLdb
help(MySQLdb.connection)
you will see some docs like the below:
class connection(__builtin__.object)
| Returns a MYSQL connection object. Exclusive use of
| keyword parameters strongly recommended. Consult the
| MySQL C API documentation for more details.
|
| host
| string, host to connect
|
| user
| string, user to connect as
|
| passwd
| string, password to use
|
| db
| string, database to use
|
| port
| integer, TCP/IP port to connect to
check your mysql server running on the machine which ip is 192.168.0.102
and then try to connect your mysql server by python command line interaction :
import MySQLdb
conn = MySQLdb.connect(user='myuser', password='mypassword', host='192.168.0.102', database='usersdb')
it’s should be working!
BTW, your should run your flask on all address which your machine have! like :
app.run(host='0.0.0.0', debug=True)
Error Because of password
Please try
import urllib.parse
password = ‘Ah4eg9c!5@v15{kWiT06E#S(&’
encoded_password = urllib.parse.quote_plus(password)
db =
SQLDatabase.from_uri(f"mysql://user{encoded_password}@host/databasename")
I am trying to run a web app with Flask. I have MySQL server on my device and have changed its bind address
to 192.168.0.102
.
Now, in Python I am attempting to connect with MySQLdb:
conn = MySQLdb.connect("user='myuser', password='mypassword', host='192.168.0.102', database='usersdb'")
…with Flask app running on 192.168.0.102
app.run(debug=True, host='192.168.0.102')
Now, I get this error :
OperationalError: (2005, "Unknown MySQL server host 'user='myuser', password='mypassword', host='192.168.0.102', database='usersdb'' (0)")
I don’t know if its because MySQL is not running on 192.168.0.102
or if it is a problem with Flask.
What could be the problem?
Try this:
Enter the IP address where the MySQL server is at in MySQLdb.connect:
conn = MySQLdb.connect(host="localhost", # MySQL host, usually localhost
user="myuser",
passwd="mypassword",
db="usersdb")
And use the address 192.168.0.102
for:
app.run(debug=True, host='192.168.0.102')
Sadly, you try to put a string
which is invalid host
to MySQLdb.connection
, it’s no the right params which function expect! you should read more documentations or try to use help function to deep understand this function. and What params its require and which type of param its expected.
import MySQLdb
help(MySQLdb.connection)
you will see some docs like the below:
class connection(__builtin__.object)
| Returns a MYSQL connection object. Exclusive use of
| keyword parameters strongly recommended. Consult the
| MySQL C API documentation for more details.
|
| host
| string, host to connect
|
| user
| string, user to connect as
|
| passwd
| string, password to use
|
| db
| string, database to use
|
| port
| integer, TCP/IP port to connect to
check your mysql server running on the machine which ip is 192.168.0.102
and then try to connect your mysql server by python command line interaction :
import MySQLdb
conn = MySQLdb.connect(user='myuser', password='mypassword', host='192.168.0.102', database='usersdb')
it’s should be working!
BTW, your should run your flask on all address which your machine have! like :
app.run(host='0.0.0.0', debug=True)
Error Because of password
Please try
import urllib.parse
password = ‘Ah4eg9c!5@v15{kWiT06E#S(&’
encoded_password = urllib.parse.quote_plus(password)
db =
SQLDatabase.from_uri(f"mysql://user{encoded_password}@host/databasename")