1

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

Community
  • 1
  • 1
Shankar ARUL
  • 12,642
  • 11
  • 68
  • 69

2 Answers2

0

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
Shankar ARUL
  • 12,642
  • 11
  • 68
  • 69
  • Are you saying `SQLALCHEMY_DATABASE_URI = 'mysql://root:password@localhost:5000/databasename'` worked? Having the 5000 in there seems very odd. – Doobeh Oct 24 '14 at 01:19
  • yeah strange indeed. I set a reredirect on my apache to port 5000 but not sure why MySQL had to be pointed at localhost:5000 – Shankar ARUL Oct 24 '14 at 08:02
-1

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.

Doobeh
  • 9,280
  • 39
  • 32
  • ofcourse i know that phpmyadmin is a web interface for the database ! And no i wasnt trying to point it to the web interface !! – Shankar ARUL Oct 24 '14 at 00:10