Symfony: Fix propel-insert-sql error: Can’t connect to local MySQL server

Posted April 4, 2008 by Quinn McHenry in Symfony

After building your model and the SQL code to represent it, symfony offers a mechanism to insert the SQL code directly into your configured database. If you are running MySQL locally, this should work for you with no additional configuration. If you are running MySQL on another server, as is common in shared web hosting and other environments, you’ll get an error. In the past, I’ve just grunted and sent the SQL code to the database manually, but there is an easy fix to make things work as they should.

If your MySQL instance is not running locally or is not configured to use a socket for communication (or the socket file is not in the default location) you’ll receive an error like this when you try to do a ‘symfony propel-insert-sql’ command:

Execution of target "insert-sql" failed for the following reason: /mnt/pear/php/symfony/vendor/propel-generator/build-propel.xml:296:1: [wrapped: connect failed [Native Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)] [User Info: Array]]

Getting an error like that can just kill your groove in a clock cycle. The thing is, forcing through this error and manually apply the SQL code to the database will work. If you have the correct database connection settings in config/databases.yml, symfony will correctly connect to the database for normal application operations. The problem is that Propel has its own database configuration information in config/propel.ini

All you need to do to fix this problem is copy the connection string from config/databases.yml into config/propel.ini, replacing the ‘propel.database.url’ setting which has a default value of ‘mysql://localhost/projectname’ where ‘projectname’ is the name of your symfony project.

The ‘dsn’ line in the databases.yml contains the connection string, something like:

dsn: mysql://username:[email protected]/dbname

Leave the ‘dsn:’ part there and copy the ‘mysql://….’ and remainder of the line and replace the connection string in the config/propel.ini line that starts with ‘propel.database.url’ with your connection string, like:

propel.database.url = mysql://username:[email protected]/dbname

If the connection string is correct (and your schema.yml file is in order) you should now be able to do a ‘symfony propel-insert-sql’ without error.


About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

Leave a Reply