Symfony2 Database Config - php

I've just inherited a Symfony2 project from a former co-worker. I'm trying to get it running locally, but am getting a SQLSTATE[HY000] [2002] Connection refused error. I guess I'm missing the database connection that I've read should be in the app/config/parameters.yml file.
Is there anyway of finding this information if all I have is the project and the live site?

If it's standard Symfony2 project you have to configure your database connection information in app/config/parameters.yml, like:
# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_name: test_project
database_user: root
database_password: password
# ...
More in doc.
After that you have to create database:
$ php app/console doctrine:database:create
Next, create table schema:
$ php app/console doctrine:schema:update --force
Also, you could load (if your co-worker provided it ; ) some data fixtures, so try:
$ php app/console doctrine:fixtures:load
If your coworker doesn't use doctrine fixtures you should consider dumping data from your prod database.

Related

Doctrine won't connect to MAMP mysql database

EDIT: I have since resolved my issue thanks to this answer https://stackoverflow.com/a/50908912/12296041
I have started learning Symfony for a school project, and I was following a tutorial from Symfony's website, but for some reason, Doctrine doesn't manage to connect to MySQL database I have running on my computer. I'm on a Macbook, using MAMP to run a local MySQL server.
Whenever I try to execute any doctrine commands that interact with the database such as php bin/console doctrine:database:create it never works.
So far, I have checked that I could indeed connect to the database using PHPMyAdmin. I have also tried to change the DATABASE_URL in the .env file, but this hasn't solved my issue.
I have also tried creating a symbolic link with sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock but that didn't work either.
This is what my .env file looks like:
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# For a PostgreSQL database, use: "postgresql://db_user:db_password#127.0.0.1:5432/db_name?serverVersion=11"
# IMPORTANT: You MUST also configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://root:root#127.0.0.1:8888/db
###< doctrine/doctrine-bundle ###
And I get this error when trying to create a database with doctrine:
In AbstractMySQLDriver.php line 93:
An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] No such file or directory
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] No such file or directory
Anything that can point me in the right direction is greatly appreciated, thanks!
I agree with Cid, port is 8889 by default for MySQL in MAMP.
You can also try to replace 127.0.0.1 by localhost.
Let me know if this works.

Gitlab CI Symfony : SQLSTATE[HY000] [2002] Connection refused

I use gitlab to run unit tests each time someone push the code. I get this error during composer installation.
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Creating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [2002] Connection refused
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception
Here is my configuration :
.gitlab-ci.yml file
# Select image from https://hub.docker.com/_/php/
image: php:5.6
# Select what we should cache
cache:
paths:
- vendor/
before_script:
# Install ssh-agent if not already installed, it is required by Docker.
# (change apt-get to yum if you use a CentOS-based image)
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
#
Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)
# Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
- ssh-add <(echo "$SSH_PRIVATE_KEY")
# For Docker builds disable host key checking. Be aware that by adding that
# you are suspectible to man-in-the-middle attacks.
# WARNING: Use this only with the Docker executor, if you use it with shell
# you will overwrite your user's SSH config.
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
- cp ci/custom.ini /usr/local/etc/php/conf.d/custom.ini
- bash ci/docker_install.sh > /dev/null
# Install composer
- curl -sS https://getcomposer.org/installer | php
services:
- mysql:latest
variables:
# Configure mysql service (https://hub.docker.com/_/mysql/)
MYSQL_DATABASE: symfony
MYSQL_ROOT_PASSWORD: root
# We test PHP5.6 (the default) with MySQL
test:mysql:
script:
# Install all project dependencies
- php composer.phar install
- phpunit --coverage-text --colors=never -c app/
parameters.yml.dist
parameters:
database_host: 127.0.0.1
database_port: ~
database_name: symfony
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: ~
mailer_password: ~
# A secret key that's used to generate certain security-related tokens
secret: ThisTokenIsNotSoSecretChangeIt
database_slave1_host: 127.0.0.1
database_slave1_port: ~
database_slave1_name: symfony
database_slave1_user: root
database_slave1_password: root
I have read and follow the instruction of the gitlab website. Maybe my mistake is obvious, but I can't see it.
As you are using MySQL that is running in another container, you have to use its hostname, not 127.0.0.1. The correct database host should be "mysql". This is covered in one of the sections of the GitLab's documentation:
The service container for MySQL will be accessible under the hostname mysql. So, in order to access your database service you have to connect to the host named mysql instead of a socket or localhost.
One of the possible reasons for this error is that you attempt to access the database while it still initialises. This is covered in the MySQL's caveats section on the Docker HUB.
If there is no database initialised when the container starts, then a default database will be created. While this is the expected behaviour, this means that it will not accept incoming connections until such initialisation completes. This may cause issues when using automation tools...
A crude solution would be to use sleep command before you start any process that accesses database. You can add it to the before_script section:
before_script:
- sleep 60s
A better and more complex solution would be to probe the MySQL server, repeatedly checking whether it already accepts connections.

Symfony with MSSQL (sqlsrv) results in timeout

I am trying to connect Symfony 2.8 to Microsoft SQL Server 2012 by using sqlsrv extension (no pdo sqlsrv, because for php7 - which I am using - there is currently only non pdo).
Trying to run cache clear or access the site in browser results in
"PDO::__construct(): MySQL server has gone away".
When I run the doctrine schema update command, everything works fine and the tables are being generated. Does anyone know how to fix the error? (I don't even know why it says Mysql server gone away when I try to connect on microsoft sql..)
Config:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: sqlsrv
host: testhost
dbname: testdb
user: testuser
password: testpw
mapping_types:
timestamp: string
I am looking for a solution since Friday but was not able to find one. I tried to select some entries from a seperate (non symfony) php skript, and there I got the results and no timeout. Why is Symfony / Doctrine giving me this strange error?
you can try to launch this command for doctrine :
php app/console doctrine:cache:clear-metadata
php app/console doctrine:cache:clear-query
php app/console doctrine:cache:clear-result
Also perhaps upgrade and downgrade your doctrine bundle for restore a correct and initial version of bundle for your project.

Symfony2 database configuration during deployment

hi i'm new to symfony2 web development. I have develop a sample symfony2 application with a database on my localhost. It is working fine on my localhost.
I have uploaded my project to web server, All the files are working on the web server other than the files connecting with databases. When I requested for the files that are connected with database I got these errors.
Warning: PDO::__construct(): MySQL server has gone away in
/home/pipeline/public_html/BusinessPipeline/BusinessPipeline/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php
on line 40
Warning: PDO::__construct(): Error while reading greeting packet.
PID=18547 in
/home/pipeline/public_html/BusinessPipeline/BusinessPipeline/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php
on line 40
I have setup the parameters.yml file as follows.
# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_name: test_project
database_user: my_username
database_password: my_password
I didn't change anything rather than change the parameters.yml.
what can I do for this issue, do I need to do any configuration prior to deployment?
Thanks in advance
Check MySQL Server doc for specific problem about your error message:
B.5.2.9 MySQL server has gone away
Check the wait_timeout problem

How do I set up a database for Symfony2 using Doctrine?

I'm following the Symfony book and arrived at the chapter on dealing with the database. I have a few questions, and can't seem to find an answer.
I'm just following the parameters.yml file that I have looks like this (auto-generated)
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: myproject
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: ThisTokenIsNotSoSecretChangeIt
debug_toolbar: true
debug_redirects: false
use_assetic_controller: true
Then I head over to the terminal and enter:
php app/console doctrine:database:create
I receive the following error:
Could not create database for connection named `myproject`
SQLSTATE[HY000] [2002] Connection refused
I have a few questions:
How do I fix this
Where is the database server? Did it come when I installed Symfony2 with Composer?
After a long search I found it. It had something to do with the mysql.default_socket as pointed out in the comments.
MAMP showed me the socket was
':/Applications/MAMP/tmp/mysql/mysql.sock'
After doing some more searching, I found that the socket can be set in config.yml, here:
doctrine:
dbal:
driver: "%database_driver%"
[...]
unix_socket: "/Applications/MAMP/tmp/mysql/mysql.sock"
[...]
If the unix_socket line is not there, just add it. Then tried to execute the same command in Terminal, which gave this result:
Created database for connection named `myproject`
And the database was really created.
Symfony doesn't install mysql I assume you are working in a lamp stack.
If I remember well it happened to me once and I changed database_host: 127.0.0.1 to localhost and it worked well since.

Categories