1
votes

Im having trouble connecting to my sql server on digital ocean through my flask webapp. Im using flask-sqlalchemy to bind the mysql database to Flask.

Im able to access the mysql server through the phpmyadmin interface at myipaddress:5000/phpmyadmin

Since Im using Nginx (I bound it to port 80). Bound Apache to Port 5000. So my phpmyadmin interface is accessible at

myipaddress:5000/phpmyadmin

In my flask app, i specify the

SQLALCHEMY_DATABASE_URI ='mysql://root:password@myipaddress:5000/databasename'

when i try to create the tables on my database using the shell with db.create_all() - it just doesnt respond. The cursor blinks forever and then i get the operational error that i quote on the title afte a few minutes

Im able to get the same setup running on my local dev machine. So i know its not a flask configuration problem but just a mysql access issue. I have my webapp up on digitalocean (Not sure if mysql server is behind a firewall or something like that making it inaccessible

On the

/etc/mysql/my.cnf

for the bind-address under mysql_d section, i tried all possible combinations and restarted the mysql server with no success

i tried localhost, 127.0.0.1, 127.0.0.1:5000, myipaddress for the bind-address (Also tried commenting it out) without any results.

Also i tried to get the user, current_user on the table properties from the mysql command line, it's listed as root@localhost for both

From this post:Lost connection to MySQL server at 'reading initial communication packet', system error: 0; i get the idea that its related to firewall but not sure how to rectify this.

Anyidea how can i connect my flask app to the mysql server ? Any help would be much appreciated

2

2 Answers

0
votes

I was specifying the mysql address as

mysql://root:password@myipaddress:5000/databasename

But since my flask app and the mysql server are running on the same server, the flask app was be able to access the mysql server when i replaced the myipaddress:5000 with localhost:5000

mysql://root:password@localhost:5000/databasename
-1
votes

The phpmyadmin is just a web interface for your database, if you're looking at connecting your Flask application to your MySql database, you need to point the SQLALCHEMY_DATABASE_URI to the actual MySQL database, rather then the web interface to that database.

Usually MySql runs on port 3306. I believe that SQLAlchemy is clever enough to know that's the default, so your connection string should just be: SQLALCHEMY_DATABASE_URI ='mysql://root:password@localhost/databasename' or if you are running on a different port/external IP address: SQLALCHEMY_DATABASE_URI ='mysql://root:password@myipaddress:4444/databasename'. Remember, if you're connecting to a MySQL database on an external server (not the same one as your Flask app is running on) you will have to change the configuration to allow that kind of access.