Forums

Web2py app connecting to mysql database

I installed a new web2py app and tried to connect to mysql database the same way I connected my first app which worked fine. This one is not working. I get an error, in the error log I see this: ERROR:root:RuntimeError: generator ignored GeneratorExit (if I am looking at the right error msg because there are so many)

Obviously I am doing something wrong but I have no idea what. When I use the sqlite development database as I originally installed the app it works on the server with no problem but when I replace the db = DAL() connection for the mysql I have no access to the site, it's crashing

Again, the funny thing is the my first app on the server is still connected to mysql and works fine and I tried to use the exact same code to connect this new app but now it doesn't work.

What am I missing?

Originally I created a msql database when I first signed up to pythonanywhere a long time ago. That works fine. Now I created a second mysql database for my new app. Here is where I am confused. I did not set a new password for the new mysql so both databases have the same password. I tried to connect the new app to both but neither one works.

How can I get some real advise or consulting on this because I obviously don't understand how I am supposed to set this up, although I set it up easily for the first time for the first app which still works fine.

The generator exit error is because the request is taking too long and the client (in this case, our loadbalancer) is giving up.

As to what you're missing, it's not really possible for us to tell from what you've given us.

You could try codementor (there's a link at the top of the main Forum page) for focussed help.

Glenn, I try to simplify the issue:

a) I connected a web2py app to the mysql database very easily and it works fine b) I tried to connect an other web2py app to the mysql database the same way I did the first one and this one doesn't work.

You're referring me to Codementor with this, but for $25 per 15 minutes on Codementor I can only present the above a) and b) and that doesn't sound like a question I should ask on Codementor. It sounds more like something the hosting support would be more equipped to help me with.

Is it possible that my app's database is too big? It's over 800MB. Could that be the problem?

Without seeing some code and an error message, it's really not possible for us to work out what's going wrong.

This is the code I use in my web2py app to connect to mysql:

db = DAL('mysql://username:password@username.mysql.pythonanywhere-services.com/username$databasename')

This works in one of the apps and doesn't work in the other app. The error message from the server log, as I mentioned before:

ERROR:root:RuntimeError: generator ignored GeneratorExit

The difference between the 2 apps:

The app that is able to connect to the mysql is small and it was installed using the web2py admin interface.

The app that is NOT able to connect to the mysql is over 800MB and because of pythonanywhere size limitation to upload using the web2py admin interface, I had to upload it using sftp. This app is currently deployed on pythonanywhere using sqlite which works fine. Here is the sqlite code I am using:

db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])

Of course, I can't use sqlite in production for this app, I need use the mysql databse to which I can not connect to.

ERROR:root:RuntimeError: generator ignored GeneratorExit is not a database error message. It is the result of our infrastructure deciding that a request has taken too long. What is the error message you get from web2py when you attempt to connect to the database? Have you tried connecting from a command line? Have you had a look at the database to ensure that everything that you expect to be there, is actually there?

Glenn, thanks again for trying to work this out.

The web2py error message is: <class '_mysql_exceptions.OperationalError'> (1050, "Table 'auth_user' already exists")

As I mentioned in the beginning, I created a second mysql but I wasn't sure if I need to have a different password for this so I didn't add a new password for the second one. However, I can't connect this app to either mysql I created. I have an other app that's connected to the first one. This maybe the problem?

Pls advise.

First thing - you are connecting to the database. Second - It looks to me like the app is trying to create a table that is already in the database.

I don't know much about the web2py DAL, but I think each app has some idea of what the database should look like and if it doesn't match, then the app tries to bring the database to match it. Maybe there's something you can do to let your app know what's already in the database.

I would suggest asking on the web2py forums, since the guys there are likely to know more about how the DAL works.

Thanks Glenn, I appreciate it. I will look into this and try to figure it out.

In the mean time can you please clarify how I am supposed to create multiple databases on pythonanywhere?

The first time, I created one database by entering the database name and created a Mysql password. The second time when I create a second database, after entering the new database name, should I create a new Mysql password for this second database as well or not?

The password you have is for all your databases. You don't need to set it for each one.

Hi Glenn, I created a new database to see if I get the same error but now there is no error. However, the issue is that when I connect the app to the mysql and I check the web2py database administration interface, the tables are all there as expected, but all the records are missing. The app was uploaded to Pyuthonanywhere using sftp because you have an app size limit for the admin interface upload. In any case, all the records were uploaded, I can run the app on your service using sqlite but they are not showing up on the mysql. Any help would be greatly appreciated.

Hi, kakasFDD310.

Did you actually insert your records to the MySQL database?

One possibility is that your SQLite database contains all the records, because you added them locally and then uploaded your database file to PythonAnywhere. Since MySQL works in a different way than SQLite (records are stored elsewhere), you need to insert your records again - this time to your MySQL database on PythonAnywhere.

I've had a similar problem with web2py -- the DAL is trying to migrate/create tables in your existing database, but the tables already exist (auth_user already exists). Best way to solve this is to export your database to csv, then delete all of the files in the databases table and change the name of your database so you start fresh (e.g., webapp2 instead of webapp). Then have web2py's DAL create the tables. Then import the database by running db.import_from_csv_file(open('somefile.csv', 'rb')).

From another forum post: Export all data in CSV format. Open console and navigate to the web2py folder. Start web2py in interactive console mode with:

python web2py.py -S your_app_name -M –P

export data in csv format with

db.export_to_csv_file(open('your_app_name_export.csv', 'wb'))

[this stores the file in the root of the web2py directory]

exit web2py interactive console mode with

exit()

Prepare web2py application for new database and create new database. In console, navigate to application folder. Backup existing database folder (and corresponding .table files) with:

 cp -r databases databases_bak

Create empty databases folder with:

rm -r databases
mkdir databases

Change DAL connection string in web2py app to:

db = DAL('mysql://user_name:password@mysql.server/my_new_database_name')

Create new empty mysql database schema (from control panel in pythonanywhere or mysql command prompt)

Generate database tables by starting web2py in interactive console mode with:

python web2py.py -S your_app_name -M –P

[this will execute the models and generate the mysql database tables and the .table files in the database directory]

Import data in csv format with:

db.import_from_csv_file(open('your_app_name_export.csv', 'rb'))
db.commit() # this is missing from some of the other instructions but is required

Exit web2py interactive console mode with:

exit()

Celebrate!

Hi guys , please how can I connect mysql with web2py?

The best place to learn about that is either the web2py documentation or in a web2py tutorial -- Killer Web Development is a popular one.