My stack is quite common, one container for my Symfony app, another one for Mysql.
For some reason, Symfony can't connect to Mysql container with following error :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
A simple php file to test the MySQL connection, on the Symfony container works with this
<?php
$dbh = new PDO('mysql:host=database;dbname=mysite', 'mysite', 'password');
Here is my Symfony parameters file:
database_driver: pdo_mysql
database_host: database
database_port: 3306
database_name: mysite
database_user: mysite
database_password: password
And here is my docker-compose file :
site:
build: webapp
ports :
- "80:80"
volumes:
- /home/myuser/dev/mysite:/var/www/html/
links:
- database
database:
image: mysql:5.5
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=mysite
- MYSQL_USER=mysite
- MYSQL_PASSWORD=password
I also tried with a fresh Symfony project without error.
Remove the database driver and port to use. As you didn't use port forwarding, you don't have to specify the port for MySQL. Also, there's a mistake into your driver, it's not just pdo but pdo_mysql (documentation).
database_host: database
database_name: mysite
database_user: mysite
database_password: password
Also, why do you specify a database_path ? MySQL doesn't need because you call it by TCP/IP. Old SQLite database ?
You could also checkout your config.yml file :
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
Related
I'm trying to connect to my aws rds database with symfony. But I constantly get an error: An exception occurred in driver: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.". My security groups are open to all ports and here are my settings.
//services.yaml
parameters:
database_driver: pdo_mysql
database_host: '%env(RDS_HOSTNAME)%'
database_port: '%env(RDS_PORT)%'
database_name: '%env(RDS_DB_NAME)%'
database_user: '%env(RDS_USERNAME)%'
database_password: '%env(RDS_PASSWORD)%'
//doctrine.yaml
doctrine:
dbal:
# if the url option is specified, it will override the above config
default_connection: default
connections:
default:
dbname: '%env(RDS_DB_NAME)%'
user: '%env(RDS_USERNAME)%'
password: '%env(RDS_PASSWORD)%'
host: '%env(RDS_HOSTNAME)%'
driver: 'pdo_mysql'
port: 3306
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
In .env you are the correct variables.
If someone has a fix, please provide it to me :)
I found the problem.
Currently, I'm a student and living in a student dorm. My dorm network is blocking the connection with an outside MySql server so that's why I repeatedly get this error.
The solution would be to use a non-public source of the internet or try to share internet from some other device. You could also use a VPN (make sure to test first because with some of them the same error ocurrs).
Lately I have created an extra database with it's own user. Therefore I have created an extra database driver in the parameters.yml. This is, as far as I know, the standard approach for this kind of situations. So far, it works. In one of the services I created, I can use this database driver. When running the code on the website, there are no problems at all.
But of course there is a problem, otherwise I won't asking for your guys help.
I'm trying to install a plugin by running the following command:
$ ./composer.phar require pugx/autocompleter-bundle
This gives the following error:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_driver_geo". Did you mean this: "database_driver"?
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-update-cmd event terminated with an exception
Installation failed, reverting ./composer.json to its original content.
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command.
Some other posts say that the error about the cache has something to do with the file/dir rights. But that doesn't seem to be the problem, because when the configuration of the geo driver is removed, these kind of errors do not appear.
I'm running Symfony 2.5
[EDIT: Added parameters.yml file]
My parameters.yml looks like this:
# This file is auto-generated during the composer install
parameters:
# Default database
database_driver: pdo_mysql
database_host: ***
database_port: ***
database_name: ***
database_user: ***
database_password: ***
# Geo database
database_driver_geo: pdo_mysql
database_host_geo: ***
database_port_geo: ***
database_name_geo: ***
database_user_geo: ***
database_password_geo: ***
mailer_transport: ***
mailer_host: ***
mailer_user: ***
mailer_password: ***
locale: ***
secret: ***
[EDIT: Added config.yml file]
The doctrine section in the config.yml file:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
mapping_types:
enum: string
bit: integer
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: %kernel.root_dir%/data/data.db3
# path: %database_path%
geo:
driver: %database_driver_geo%
host: %database_host_geo%
port: %database_port_geo%
dbname: %database_name_geo%
user: %database_user_geo%
password: %database_password_geo%
charset: UTF8
mapping_types:
enum: string
bit: integer
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: %kernel.root_dir%/data/data.db3
# path: %database_path%
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
***CoreBundle: ~
geo:
connection: geo
mappings:
***GeoBundle: ~
auto_generate_proxy_classes: %kernel.debug%
I hope there's someone that can help me fix this problem.
Kind regards,
Malcolm
As mentioned in comments, parameters.yml file is autmatically rebuilt after composer update or install command. You can see that in your composer.json file in scripts section:
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
// other commands...
],
"post-update-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
// other commands...
]
},
You can of course turn off this feature if you don't like it. But it may be useful when used properly.
Therefore when you install some package via composer you're losing parameters that you put directly into parameters.yml.
What you should do is to make use of parameters.yml.dist file which is used to build parameters.yml. It should provide application parameters values (if they are the same for every instance of app) or default values if parameters are different for every environment (prod/dev).
In your case it's the second use case (default values), since DB credentials will change for every server. It's actually exactly the same as configuration of default DB connection. parameters.yml.dist contains some default values for these params.
I can't found a way to setup Symfony with a postgresql database on OS X 10.10 (clean install). Here is what I have done:
1) Install PHP 5.6 from Liip (specially built for Symfony): http://php-osx.liip.ch/
2) Install Postgres.app: http://postgresapp.com/
3) Install Symfony: http://symfony.com/
4) Setup my parameters.yml
parameters:
database_driver: pdo_pgsql
database_host: 127.0.0.1
database_port: 5432
database_name: bachelor
database_user: username
database_password: ~
I have created the DB, verified that I have the right PHP cli, the right pgql cli, etc.
I have created a bundle in Symfony and some entities (no errors here, it works with MySQL).
But when I launch any command to interact with the DB like "php app/console doctrine:schema:update --dump-sql", I have this error:
[PDOException]
SQLSTATE[HY000] [2006] MySQL server has gone away
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: PDO::__construct(): MySQL server has gone away
But why? I don't use a MySQL server...
Does someone have any solution?
Thanks!
Can you show us your /app/config/config.yml ? please
Did you change the dbal connection value ?
# Doctrine Configuration
doctrine:
dbal:
default_connection: pgsql
connections:
#Postgresql
pgsql:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
I come here for the first time because I have a problem when I want to create my database with doctrine and Symfony. When I want export my classes in my database I have this problem. I think everything is okay, doctrine find my base but can't create it. Also, I created my database named "symfony" in phpmyadmin.
I hope someone can help me, thank you so much :).
php app/console doctrine:database:create
Could not create database for connection named `symfony`
An exception occurred while executing 'CREATE DATABASE `symfony`':
SQLSTATE[HY000]: General error: 1007 Can't create database 'symfony'; database e
xists
Configuration file:
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: 255a33a57b471045aa29326785659c6b0
database_path: null
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
To summarize:
The error informs you that a database with the same name was already created. There are three options:
(1) Delete your current database without saving it, using MySQL command line. If you want to delete your database run: DROP database name_database
(2) Delete the database without saving it, using console: C:\wamp\www\Symfony>php app/console doctrine:database:drop
(3) In the case you like to save the content of your current database. Rename you current database. As far as I know you cannot do this directly. This is a work around. First, create a new database using command line: CREATE DATABASE name_old_database;. Second, copy all tables to the new database RENAME TABLE name_database.name_table TO name_old_database.name_table;. Finally,DROP DATABASE name_database
See further info: How do I quickly rename a MySQL database (change schema name)?
I understand why I should keep my database connections in the parameters.yml file but I also wanted to setup additional database connections as well as different hosts for dev, stage and prod environments. I thought I could do this with the config_dev.yml, config_test.yml and config_prod.yml in conjunction with config.yml, am I wrong?
In parameters.yml I hold the main database connection but can I move this to the config(s) instead?
Example: config.yml
# Doctrine Configuration - notice there is no host defined here
doctrine:
dbal:
default_connection: blah
connections:
blah:
driver: pdo_pgsql
port: 5432
dbname: blah
user: blah_user
password: blah_pass
charset: UTF8
foo:
driver: pdo_pgsql
port: 5432
dbname: foo
user: foo_user
password: foo_pass
charset: UTF8
Example: config_dev.yml
# Doctrine Configuration - Here I define the dev hosts
doctrine:
dbal:
connections:
blah:
host: blah_dev
foo:
host: foo_dev
Example: config_test.yml
# Doctrine Configuration - Here I define the stage or QA hosts
doctrine:
dbal:
connections:
blah:
host: blah_stage
foo:
host: foo_stage
Example: config_prod.yml
# Doctrine Configuration - Here I define the prod hosts
doctrine:
dbal:
connections:
blah:
host: blah_prod
foo:
host: foo_prod
Now I have also removed the settings in parameters.yml but Symfony / Doctrine doesn't like this. Am I missing something? How can I setup something like I have?
Now if I define the default database connection in parameters.yml I can connect to it and then
parameters:
database_driver: pdo_pgsql
database_host: blah_prod
database_port: 5432
database_name: blah
database_user: blah_user
database_password: blah_pass
and in config.yml
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
But now I loose the multiple databases I need and the ability to switch from dev to stage to prod database servers.
I must be missing some additional documentation where this is being addressed, any help would be great.
I've seen the documentation for Multiple Database connections with Doctrine
http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html
But I would like to avoid having to add three database options like below for each dev, stage and prod server(s) in parameters.yml
parameters:
database_driver: pdo_pgsql
database_host: blah_dev
database_port: 5432
database_name: blah
database_user: blah_user
database_password: blah_pass
#database_driver2: pdo_pgsql
database_host2: blah_stage
#database_port2: 5432
#database_name2: blah
#database_user2: blah_user
#database_password2: blah_pass
#database_driver3: pdo_pgsql
database_host3: blah_prod
#database_port3: 5432
#database_name3: blah
#database_user3: blah_user
#database_password3: blah_pass
( Maybe just configure the different hosts? )
This looks ugly and a PITA, and this is just for blah database, I would have to do this for foo and any other databases I need to configure.
If your connections are the same in each environment but only the configuration values are different, you should use the parameters-configuration-file. Define the environment variables, you need, in the parameters.yml.dist file like this:
# parameters.yml.dist
parameters:
database_host_1: blah
database_host_2: blub
...
Your config.ml may look like this:
# config.yml
imports:
- { resource: parameters.yml }
...
doctrine:
dbal:
default_connection: blah
connections:
blah:
driver: pdo_pgsql
port: 5432
host: "%database_host_1%"
dbname: blah
user: blah_user
password: blah_pass
charset: UTF8
foo:
driver: pdo_pgsql
port: 5432
host: "%database_host_2%"
dbname: foo
user: foo_user
password: foo_pass
charset: UTF8
If your parameters.yml is missing, you'll be ask for the database hostnames next time you call composer update for example.
If you would like to save all your configurations in separate files, you should add this at the beginning of your config_XXX.yml files:
# config_XXX.yml
imports:
- { resource: config.yml }
- { resource: parameters_XXX.yml }
Replace XXX with your environment. Create a parameters_XXX.yml for each environment and set the configuration parameters for your hosts in there like it is in the default parameters.yml file.
# parameters_XXX.yml
parameters:
database_host_1: blahInXXXEnv
database_host_2: blubInXXXEnv
...
I don't see there a reason to use different prod and dev parameters config files.
You can use specific parameters.yml.dist
http://symfony.com/doc/master/cookbook/workflow/new_project_git.html
file at your PC with all defined connections.
Multiple connetions and work with them:
http://symfony.com/doc/master/reference/configuration/doctrine.html
http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html