Symfony 5 : SQLSTATE[HY000] [2002] refusing connection - mysql

I aam trying to create a new doctrine database using Symfony 5.
Recently Symfony changed using mysql by default to using pstgreSQL.
As I want to use mysql I changed the comment in .env from :
# DATABASE_URL="mysql://db_user:db_password#127.0.0.1:3306/db_name?serverVersion=5.7"
DATABASE_URL="postgresql://db_user:db_password#127.0.0.1:5432/db_name?serverVersion=13&charset=utf8"
to
# DATABASE_URL="mysql://root:#127.0.0.1:3306/mywebsite?serverVersion=5.7"
DATABASE_URL="mysql://root:d#127.0.0.1:5432/mywebsite?serverVersion=13&charset=utf8"
in other words I just changed postgresql to mysql and commented what needs to be commented.
However when I run
php bin/console doctrine:database:create
I get the following error :
In AbstractMySQLDriver.php line 112:
An exception occurred in driver: SQLSTATE[HY000] [2002] No connection
has been established as the target computer rejected it (translated
from french)
I found the following posts :
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
and
In AbstractMySQLDriver.php line 112:
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr
esses: getaddrinfo failed: Hte inconnu.
in short both tells me to change "127.0.0.1" by "mysql"
but when running as such I get this error
In AbstractMySQLDriver.php line 112:
An exception occurred in driver: SQLSTATE[HY000] [2002]
php_network_getaddr esses: getaddrinfo failed: Hte inconnu.
What mistake did I do or what did I miss?
Thank you

I found a very simple solutionthat will seems obvious for experienced dev but not for beginners that is never talked about.
just change the # at the beginning of the first line
# DATABASE_URL="mysql://root:#127.0.0.1:3306/mywebsite?serverVersion=5.7"
DATABASE_URL="mysql://root:d#127.0.0.1:5432/mywebsite?serverVersion=13&charset=utf8"
to the second line
DATABASE_URL="mysql://root:#127.0.0.1:3306/mywebsite?serverVersion=5.7"
# DATABASE_URL="mysql://root:d#127.0.0.1:5432/mywebsite?serverVersion=13&charset=utf8"

You can found documentation here https://www.doctrine-project.org/projects/doctrine1/en/latest/manual/introduction-to-connections.html
In your config the port 5432 is not default port of Mysql
Try
DATABASE_URL="mysql://root:d#127.0.0.1:3306/mywebsite?serverVersion=13&charset=utf8"

Related

getaddrinfo failed while trying to connect to the database

My hosting provider is giving me a host for my database.
But I can't access this host from my symfony project:
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
here is my .env:
DATABASE_URL=mysql://esoh_metask:mypassword#esoh.myd.infomaniak.com:3306/esoh_task_db
When i ping my host, i get an error saying that is it not known.
The hostname esoh.myd.infomaniak.com resolves to the IP address 10.4.27.10.
10.*.*.* addresses are all for private use within data centers. So, if you're trying to run your symfony program on your own machine (office or home) you won't be able to reach that MySQL server -- it's behind a firewall at your vendor.
Ask your vendor for help.

Shopware Docker Can't login into Docker MySQL database

Hi i installed Shopware developer project like here and it works fine but i can't login into database. I am getting error if i tried ./psh.phar administration:watch or ./psh.phar administration:build ->
In AbstractMySQLDriver.php line 93:
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr
esses: getaddrinfo failed: nodename nor servname provided, or not known
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodena
me nor servname provided, or not known
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodena
me nor servname provided, or not known
In PDOConnection.php line 27:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename
nor servname provided, or not known
Execution aborted, a subcommand failed!
I tried also login in adminer with credentials what are in docker-compose.yml and i am getting same error. Do you have any ideas? 😇
UPDATE
I had to first time start ./psh.phar docker:ssh and then follow commannd ./psh.phar administration:build. Now everythink working fine :)
The Docker Hub page for MySQL has many useful information on how to use MySQL with Docker.
In your docker-compose/Dockerfile filess make sure to provide the environment-variables required by MySQL, e.g.: MYSQL_ROOT_PASSWORD.
Also, make sure to read Shopware documentation to make sure you are providing all the required values in your docker-compose/Dockerfile files.
In my docker-compose.yml i have this enviroment configuration
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: app
MYSQL_PASSWORD: app
If i launch my app on localhost:8000 evereythink working so it's mein that app can log into database but from console or adminer i can't. :-/
Please take a look weather the mysql container started. You can use docker ps -a for it.
If not, use docker logs ${containerId} for showing information why it not started.
Also post a ./psh.phar docker:start here.

Running database migrations during docker-compose build fails, but it works from command line

I have a docker-compose.yml and a Dockerfile that create containers for an app in Symfony 2.8.
The containers are called: webserver (Nginx), app (Symfony) and db (MySql).
When I create the containers, I can easily access the bash of the app and run php app/console doctrine:migrations:migrate. Everything works fine.
I would like to insert the command in the Dockerfile, like this:
RUN cd /var/www && php app/console doctrine:migrations:migrate
but, when it arrives at that point, it just returns:
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
[PDOException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
[PDOException]
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
doctrine:migrations:migrate [--write-sql] [--dry-run] [--query-time] [--allow-no-migration] [--configuration [CONFIGURATION]] [--db-configuration [DB-CONFIGURATION]] [--db DB] [--em EM] [--shard SHARD] [--] [<version>]
ERROR: Service 'app' failed to build: The command '/bin/sh -c cd /var/www && php app/console doctrine:migrations:migrate' returned a non-zero code: 1
What could the problem be? It works after I access the bash in the container, but not automatically from the script.
While the image is being built, it's not yet running on a container and attached to the network to be able to see the other containers defined in our docker-compose.yml file.
Not only doing DB migration during the image build is a bad idea: it's not possible if the DB it depends on it is running on a different container and trying to reach it through its internal network name.
If the DB server is connected, which was already running and had a publicly reachable hostname, and the project was configured to use that server instead of one running in another container, it would work. But it would still be a bad idea.

Wordpress php_network_getaddresses: getaddrinfo failed: Temporary failure , but other attempts to connect with mysql work fine

I get the following message when I access a wordpress server hosted on EC2 with a security group that has access to the RDS instance.
Warning: mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in [...]/wp-includes/wp-db.php on line 1531
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in [...]/wp-includes/wp-db.php on line 1562
Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in [...]/wp-includes/wp-db.php on line 1562
Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in[...]/wp-includes/wp-db.php on line 1562
If I create a test file using the same login info but mysqli instead of wordpress to connect I get a successful connection, similarly, using the same login info but with the mysql cli tool, I get a successful connection.
<?
$mysqli = new mysqli("xxx.cluster-xxx.us-east-2.rds.amazonaws.com", "root", "xxx", "dbname");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
Looking for guidance as to why the mysql connection through wordpress fails like this when other attempts to connect using the same info do not.

SMTP mail error in Opencart 2.0.3.1

When I try submit email in contact page I have get following error in OpenCart 2.0.3.1
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed:
Name or service not known in /[path]/system/library/mail.php on line
170 Warning: fsockopen(): unable to connect to ssl://smtp.gmail.com
:465 (php_network_getaddresses: getaddrinfo failed: Name or service
not known) in /[path]/public_html/muadoan_com/system/library/mail.php
on line 170 Notice: Error: php_network_getaddresses: getaddrinfo
failed: Name or service not known (0) in
/[path]/public_html/muadoan_com/system/library/mail.php on line 173
In setting, I set protocol SMTP,
hostname : ssl://smtp.gmail.com
username : myemail#gmail.com
password : myemailpassword
port: 465
I searched and found that someone had this issue but with the older versions of opencart.
Anyone helps me please?
Grep the code from document root directory with:
grep -R "'config_mail_smtp_host'" *
In the files found replace $this->config->get('config_mail_smtp_host') with $this->config->get('config_mail_smtp_hostname')
On this issue see here, but you can find more files, probably.