Error connecting to database in production environment - php

When launching my web app I get a 500 error. The prod.log shows a connection error to the database, and just below that there is a cache.WARNING: Failed to save key in the /var/www/project/var/cache folder. (Not sure if they are related.)
The error says getaddrinfo failed: Temporary failure in name resolution at /var/www/etc.
I've tried changing the configuration of the db connection string from localhost to 127.0.0.1 to the actual IP address and not seeing a difference.
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
.env
DATABASE_URL=mysql://username:password#127.0.0.1:3306/atlas
I'm expecting the configuration to correctly identify the localhost or local ip address and connect to the db.

The issue with db error was resolved by created a new repo for the web app and set to run using prod environment variables from the beginning. I can only assume the old files held on to some configuration somewhere that was causing the issue. With the failure to save key error I resolved that by giving read/write permissions to the projects var folder. The Syfmony docs only suggest doing this to the var/log directory however it had problems writing to other var/ directories as well.

Related

Why I can't connect the Azure MySQL database?

I have a problem connecting from eZPlatform 3.0 (based on Symfony 5) to the Azure MySQL database.
I get the error:
SQLSTATE[HY000] [9002] SSL connection is required. Please specify SSL
options and retry.
Configuration is as follows:
doctrine:
dbal:
# configure these for your database server
driver: '%database_driver%'
charset: '%database_charset%'
default_table_options:
charset: '%database_charset%'
collate: '%database_collation%'
url: '%env(resolve:DATABASE_URL)%'
options:
!php/const:PDO::MYSQL_ATTR_SSL_CA: '%ca_cert%'
!php/const:PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT : false
Strange thing is that this happens when I visit the route /admin, on homepage I don't get that exception.

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.

Symfony 4 Doctrine not working from console [2002] No such file or directory

I´m working with symfony 4 and this error ocurr when runnning doctrine console commands:
In AbstractMySQLDriver.php line 108:
An exception occurred in driver: SQLSTATE[HY000] [2002] No such file or directory
I think this is because there´s something wrong with the connection but when I run the application it has access to the database without errors, so I can not imagine what is wrong with the connection.
I was able to continue working by creating manually the database and using
php bin/console doctrine:schema:create --dump-sql
But this is really unwanted because later when the schema needs to be updated all data would be lost because i would need to re-create the schema instead of updating it.
The app works normally after executing manually the SQL (easyadmin works fine).
Here are my configurations:
.ENV:
DATABASE_URL=mysql://root:root#127.0.0.1:3306/test
doctrine.yaml:
parameters:
env(DATABASE_URL): ''
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
I tried the drop command just to test and it throwed this:
In DropDatabaseDoctrineCommand.php line 93:
Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.
So I add dbname to doctrine.yaml and didn´t worked.
I also tried removing the cache, creating a new project, using a remote mysql, and using sqlite but the errors are the same in both cases !!
Thanks in advance for the help !
Please change DB_HOST=localhost to DB_HOST=127.0.0.1
> doctrine/doctrine-bundle
DATABASE_URL=mysql://${DB_USER}:${DB_PASSWORD}#${DB_HOST}:${DB_PORT}/${DB_NAME}
So i suppose that you showed the whole content of your doctrine.yml.
If so, you might want to consider changing your doctrine.yml as follows:
add an additional line in the dbal: section
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
url: '%env(resolve:DATABASE_URL)%'
Watch the last line, beginning with url:, closely.
The 'resolve:" operator looks up DATABASE_URL in your env-Variables and if it is found, it then replaces the expression with its content.
This is a doctrine.yml, that gets auto-generated by symfony.
The 'resolve' operator migth not be needed though. This could potentially work too:
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
url: '%env(DATABASE_URL)%'
This method is proposed in the symfony docs, which might be interesting to read, to understand further configuration of symfony.
My fix is here:
export DATABASE_URL=<value from .env>
And generating SQLs works :)
config/packages/doctrine.yaml
I solved this error adding my database url in the "config/packages/doctrine.yaml" file the "parameters" > "env(DATABASE_URL)" field:
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): 'mysql://your_db_url'
I solved this change in .env file
DATABASE_URL=mysql://root:root#127.0.0.1:3306/test
to
DATABASE_URL="mysql://root:root#127.0.0.1:3306/test"
I added quotes. Maybe you are using windows? It works for me.
like in Symfony docs: Environment Variables
Hi On my side I add the exact same behaviour, on symfony 4,
I just changed this
DATABASE_URL=mysql://root:root#localhost:8889/
to this
DATABASE_URL=mysql://root:root#127.0.0.1:8889
on my .env or .env.dev file (depend on the env you are working on)
hope it helps.

Symfony - How to connect to remote mysql database without install mysql-server

I am trying to connect to a remote mysql database with my Symfony application. I don't want to install the mysql-server package on my server if I don't need to but when I try to connect, I get the following error:
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [2002] No such file or directory
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
Here's what my database configuration look like. In the variables, I setup pdo_mysql as the driver and the other fields are working well.
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
Edit1: If I install mysql-server, it works and the error is gone.
Edit2: My server is running Ubuntu 16.04.2 LTS
You should be able to install client on Ubuntu like this:
apt-get install mysql-client
I think 5.5 is the latest.
Go here for setting up MariaDB:
https://downloads.mariadb.org/mariadb/repositories/#mirror=syringa&distro=Ubuntu
makes more sense nowadays to use MariaDB instead:
EDIT #2
to add remote useron the remote system:
CREATE USER 'fooUser'#'192.168.40.200' IDENTIFIED BY 'myPassword';
check the user table to see if they can connect from remote system:
use mysql;
SELECT Host,User,Password,ssl_type from user;
ssl_type is if you are using SSL to connect, you could leave that out.

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

Categories