Move Symfony2 database to remote host - symfony

I am fairly new to Symfony2, so please bear with me. I have a Symfony2 application that is currently using a database at localhost. I moved a copy of the database to a remote host and edited my parameters.yml file to reflect the new database_host. I then ran php app/console cache:clear --env=dev --no-debug since this is a development version of the application. The application is still pulling the data from the localhost.
Do I need to clear other caching or run some other command to get the application to see the new database?
Here are the values in my parameters.yml file:
database_driver: pdo_mysql
database_host: *removed*
database_port: 3306
database_name: target
database_user: imac
database_password: *removed*
Here is the doctrine section of the config.yml
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
types:
datetime: Target\Bundle\UserBundle\Doctrine\DBAL\Types\DateTimeType

Related

Symfony 2.8 An exception ocurred in driver SQLSTATE[HY000] [2002] Connection refused Error

I want to use sqlite in my app but when I execute the command to create the database I have this error
An exception ocurred in driver SQLSTATE[HY000] [2002] Connection
refused
Command:
php app/console doctrine:database:create
parameters.yml file:
parameters:
database_host: localhost
database_name: trivago
database_user: root
database_password: password
database_path: "%kernel.root_dir%/data/data.db3"
config.yml file:
doctrine:
dbal:
driver: pdo_sqlite
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your databases 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%"
database_path in parameters.yml.dist is already uncommented.
If I use the mysql driver it creates the database correctly.
Connection refused most of the time means that your credentials are wrong or even the host. Check your parameters.yml and try to connect to your db directly with sqlite3

Symfony 2 SQLSTATE[HY000] [2002] Connection refused Error

I get an error like database operations using Symfony2.
SQLSTATE[HY000] [2002] Connection refused
parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: '8889'
database_name: symfony
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: tr
secret: ef9b4381fe75208f060b7c786951242bebcfb3c2
database_path: /private/var/mysql/mysql.sock
And console:
Kemal-Karakass-MacBook-Pro:~ kemalkarakas$ locate mysql.sock
/private/var/mysql/mysql.sock
How do I resolve the error?
i had the same issue and fixed it changing
database_host: 127.0.0.1
to
database_host: localhost
in parameters.yml
hopefully this helps
On Mac OSX using Mamp Pro, what solved the problem for me was going to the MySQL tab on the UI and checking the option Allow network access to MySQL
Don't forget to click Save to update the settings
There is a parameter unix_socket you can use within your config.yml.
See full configuration example:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
dbname: %database_name%
user: %database_user%
host: %database_host%
password: %database_password%
unix_socket: /tmp/mysql.sock
I had the same problem using MAMP because the default port is not 3306 but 8889.
You can find that information in the MAMP starting web page.
My parameters
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: '8889'
database_name: symfony_blog
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: 7f03537e81f981683bc773b9ec7113ab5861adec
database_path: null
This is a bit stupid but I arrived on this post googling the same error, so maybe this can help someone.
In case you have this Connection refused Error double check that your database is up and running, in my case I simply forgot to power on MAMP...
Had this issue "SQLSTATE[HY000] [2002] No such file or directory" on symfony 3.2.9 on deployment
I fixed it by changing the database_host value from "localhost" to my server IP on my parameters.yml file
but I had this another error message when trying to run commandline by SSH:
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused
I finaly fix it by adding these 2 lines on my config.yml file in Doctrine configuration section :
unix_socket: /var/lib/mysql/mysql.sock
server_version: '5.5'
all my final doctrine configuration is like this:
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
unix_socket: /var/lib/mysql/mysql.sock
server_version: '5.5'
charset: UTF8
hopefully this helps
After seeing the answers and playing around with this myself I figured out the problem:
The default installation of LAMPP, MAMPP has the following in the my.cnf file:
skip-networking
This means that mysql will not listen on TCP/IP so you can tell doctrine to try any port in the 64K and it won't do any good until you comment out the directive above and get mysql to listen to TCP/IP ports.
Please ensure you restart LAMPP, MAMPP or reload mysql after commenting out the skip-networking directive.
If anyone is still stuck on this issue when using a virtual environment, I found if you are attempting to run database commands on the host machine and running into the "connection refused" error, try ssh-ing into your guest machine and running the commands directly so the path to mysql.sock is correct.
(I guess it makes sense that the relative path needs to be on the virtual guest machine, not the host machine).
Configuring symfony2/doctrine to connect to MySQL using host:port
comment out doctrine.dbal.path: %database_path% in app/config/config.yml
remove/comment out the parameter database_path from app/config/parameters.yml
check your configuration files for correct indentation
wrap either none of the values in app/config/parameters.yml single quotes or put all in double quotes.
Afterwards your configuration should look like this:
Parameters
# app/config/parameters.yml
parameters:
database_driver: "pdo_mysql"
database_host: "127.0.0.1"
database_port: "8889"
database_name: "symfony"
database_user: "root"
database_password: "root"
# comment out or remove
# database_path: ~
Configuration
# app/config/config.yml
# ...
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
# path is for pdo_sqlite: i.e. %kernel.root_dir%/data/data.db3
# comment it out !
# path: ~
Running on MAMP, this is what configuration of /app/config/parameters.yml that worked for me:
parameters:
database_host: '127.0.0.1'
database_port: '8889'
database_name: test
database_user: root
database_password: root
I lost quite some time with database_host: localhost, which didn't work.
Beginners Tips:
Don't forget to remove the semicolon ; to enable extension=php_pdo_mysql.dll in php.ini (in application/MAMP/conf/phpX.X.X - check your current version in MAMP settings)
and/or in php.ini and/or php.ini.default in Macintosh HD/private/etc (use spotlight search to find this hidden file).
Today on a fresh installation of Symfony3 I had the same error:
SQLSTATE[HY000] [2002] Permission denied
Installation specs:
CentOS Linux 7.2.1511
Symfony 3.1.2
MariaDB 5.5.47
PHP 7.0.8
An earlier answer by izus did resolve the problem on my server. He suggests to change the parameter database_host from 127.0.0.1 to localhost in the Symfony configuration file app/config/parameters.yml
The real origin of the problem was not inside Symfony configuration, but authorization inside the database server. I've created the database and authorization for this database by executing the following SQL queries:
CREATE DATABASE user CHARACTER SET utf8 COLLATE utf8_bin;
GRANT ALL PRIVILEGES ON database.* TO user#localhost IDENTIFIED BY 'password';
This query implies only the host 'localhost' is authorized. This doesn't match '127.0.0.1' exactly. The database server rejects the incomming connection, and Symfony is throwing an exception and showing this error message.
Possible solutions to this specific situation:
Change the database_host parameter
Change the authorization inside the database server
In MAMP PRO I have activated "Allow Network Access to mysql" and my "php artisan migrate" is working now for that special app.

Symfony2 and postgresql

I want to create a project using symfony2 with postgresql, but when I run the command: php app/console doctrine:database:create I got the error: Could create database named "test", could not find driver, I know It has something with drivers but I made sure wampserver load php_pdo_pgsql,
this is my parameters.yml file:
database_driver: pdo_pgsql
database_host: 127.0.0.1
database_port: 5432
database_name: test
database_user: postgres
database_password: openpgpwd
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: eb10e110d2a3c3f3f5df66181eb10376109f
database_path: null
Try this. Tested on Symfony 2.7. In parameters.yml the database_driver does not work. To make postgre work with Symfony, in config.yml find
doctrine:
dbal:
driver: pdo_mysql
And change it to
doctrine:
dbal:
driver: pdo_pgsql
That should do the trick. You can get rid of database_driver in parameters.yml or use it as you change pdo_mysql to %database_driver% .
There is no need to tell that the php postgresql module is necessary.
I had the same problem, I solved it by installing php-pgsql

Symfony2 + Doctrine: Create Entities from specific database that is not the default

The Symfony2 Cookbook provides information on how to create entities from existing schemas, but I haven't been able to find a way to import from another database called 'legacy' (see config.yml below) that isn't the default.
Running the following command creates YAML files only from the default database
$ php app/console doctrine:mapping:convert yml ./src/Soapbox/DashboardBundle/Resources/config/doctrine/metadata/orm --from-database --force
If I don't provide any arguments, I receive suggestions, but not sure which ones are applicable.
doctrine:mapping:convert [--filter="..."] [--force] [--from-database] [--extend[="..."]] [--num-spaces[="..."]] [--namespace[="..."]] [--em[="..."]] to-type dest-path
app/config/config.yml
# 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
legacy:
driver: pdo_mysql
host: my.host
port: null
dbname: magazines
user: soapbox
password: XXXXX
charset: UTF8
If I'm thinking clearly which I might not be, you have two EntityManagers, default and legacy.
As such you can use the flag --em "legacy" to get it to import from that specific database.

Doctrine Migrations, problems using custom doctrine types

I'm building a application using Symfony2 + Doctrine2. My application needs to store geospatial data, so I wrote the proper doctrine extensions. All is working pretty good and the app have been running in a production enviroment for a long time.
Now I have to add some new features and I need to update the database without deleting all the data. I thougth about using the DoctrineMigrationBundle but when I run:
$ php app/console doctrine:migrations:status
I got this error:
[Doctrine\DBAL\DBALException]
Unknown database type point requested,
Doctrine\DBAL\Platforms\MySqlPlatform may not
support it.
This is the relevant section of my config.yml:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
types:
point: App\EngineBundle\DoctrineExtensions\PointType
The custom type 'point' has been mapped, so what am I doing wrong?
I answer my own question, it seems the problem is DoctrineMigrations also needs a mapping for the custom type. So the config.yml should look like this:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
types:
point: App\EngineBundle\DoctrineExtensions\PointType
mapping_types:
point: point

Resources