laravel mysql 8 connection time out - php

I'm a beginner at Laravel and configuring MySQL.
I have a VM with Laravel 5.6 and MySQL 8. I've configured MySQL so I can log in with MySQL Workbench and everything is working well, but when I try to do a query in Laravel I get an error:
Illuminate \ Database \ QueryException (2002)
SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from `users`)
My VM has a static IP of 192.168.56.3 and DHCP 192.168.56.1.
With MySQL Workbench I can connect with 192.168.56.3, but in Laravel this IP gives me an error:
PDOException (2006)
SQLSTATE[HY000] [2006] MySQL server has gone away
The other IP 192.168.56.1 gives me a timeout error.
My .env file is:
DB_CONNECTION=mysql
DB_HOST=192.168.56.3
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
and my .cnf under [mysqld] is:
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
port = 3306
basedir = /usr
tmpdir = /tmp
#bind-address = 192.168.56.3

I know it's been a while but I've found the answer. Use the following command to update your password with new authentication plugin. You can use MySQL 8 now. Have fun.
ALTER USER `username`#`localhost` IDENTIFIED WITH caching_sha2_password BY 'password';
OR
ALTER USER 'username'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Related

Ubuntu 20.4 Mysql access from remote IP with PHP issue

I want to access to MySQL db on an Ubuntu Server from another Web server.
I get my web server outgoing IP with this PHP script running on the Web server:
$realIP = file_get_contents("http://ipecho.net/plain");
echo "Real IP: $realIP<br>";
I opened the ufw port on the Mysql Server:
sudo ufw allow from realIP_result to any port 3306
I change the Mysql configuration file to accept remote connections:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
and then edit updated the bind-address to 0.0.0.0:
bind-address = 0.0.0.0
I create the user on MySQL Server:
CREATE USER 'sammy'#'remote_server_ip' IDENTIFIED WITH mysql_native_password BY 'password';
I created in this way, but I also tried without the mysql_native_password.
I grant all to that user:
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'#'remote_server_ip' WITH GRANT OPTION;
I flush privileges:
FLUSH PRIVILEGES;
And when I try to connect from the web server with this PHP script:
$conn2=mysqli_connect("MYSQLIP","sammy","password","mydbname");
// Check connection
if ($error = mysqli_connect_error($conn2)) {
echo "Failed to connect to MySQL: " . $error;
exit();
}
I get this error:
Warning: mysqli_connect(): (HY000/2002): Connection timed out in /customers/e/9/2/mysite/myscript.php on line XX Failed to connect to MySQL: Connection timed out
How can I resolve, or debug further?

MySQL8 connection with php7.3 and Symfony 5 does not work

I have a connection problem with a mysql8 database, php7.3 and Symfony 5. The user and the password are correct !
The connection to MySQL8 database must be done from an SSH tunnel
The SSH tunnel is working :
ssh -p [remote_machine_port] -N -L [mysql_tunnel_port]: 127.0.0.1: 3306 -i / [directory] / private_key [username] # [remote_machine_ip]
MySQL access configuration:
GRANT ALL PRIVILEGES ON 'databasename'.* TO '[user]'#'%' WITH GRANT OPTION;
Doctrine configuration :
DATABASE_URL='mysql://[user]:[password]#127.0.0.1:[port_tunnel_mysql]/[databasename]'
An exception occurred in driver: SQLSTATE [HY000] [1130] Host '127.0.0.1' is not allowed to connect to this MySQL server
I have the same problem with heidiSQL and on the VM
I tried with the Ip of the VM instead of 127.0.0.1
ssh -p [port_machine_distant] -N -L [port_tunnel_mysql]:[ip_machine_vm]:3306 -i /[directory]/private_key [username]#[ip_machine_distant]
Doctrine configuration
DATABASE_URL='mysql://[user]:[password]#[ip_machine_vm]:[port_tunnel_mysql]/[databasename]'
An exception occurred in driver: SQLSTATE [HY000] [2002] Connection refused
However, this setting works with heidiSQL but not with the Symfony application !
Did I forget a setting on the VM or Mysql ?
127.0.0.1 connection issue :
We have created the '[user]'#'127.0.0.1' rights.
In MySQL, 127.0.0.1 uses the network and localhost uses the socket.
The message "The server requested authentication method unknown to the client [caching_sha2_password]" was caused because the setting 'default-authentication-plugin=mysql_native_password' was created after the creation of the rights.
We regenerated the rights, and now everything is working fine.

Database doesn't connected on production environment

Setup on production server digitalocean's Ubuntu 18.04
production build - dist
php fpm php7.2 , mysql-server 5.7 , nginx , root#prod
very first I installed mysql-server on Ubuntu droplet according to this website.
https://support.rackspace.com/how-to/installing-mysql-server-on-ubuntu/
root is my username of the droplet and whenever I type mysql on the terminal it just promnt without asking any password to mysql shell
in my env.php file I have mentioned my database connection like this
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "123qwer";
$mysql_database = "xp";
$mysql_port="3306";
$conn = mysqli_connect($mysql_hostname, $mysql_user,
$mysql_password, $mysql_database,$mysql_port);
if (mysqli_connect_errno()) {
echo "Failed to connect to database: " . mysqli_connect_error();
}
after that I deployed my production build to the appropriate nginx serve root and it's serving my website but when I checked my database connection under inspect browser I can see this error
Failed to connect to database: Access denied for user 'root'#'localhost'
but when I try this on my Ubuntu server
ssh root#localhost
, password - 123qwer
this can login to localhost
what is the issue with my website please can anyone help me ?
You have to create user for root#localhost in your db and add read/write access to it.
use mysql shell on your server. sudo should be used.
login:
mysql -u root -p
If will ask for the password.
commands:
you can create or replace if the user does not exists. You can alter it if exists as well if you don't know its password. I guess it exists, so you can skip this step.
CREATE OR REPLACE USER 'root'#'localhost'
IDENTIFIED BY 'password';
Add privileges to all database. You can add one specific as well.
GRANT ALL ON *.* TO 'root'#'localhost' WITH GRANT OPTION;
I have Mariadb, can be some diff. You can use this for mysql if the one above doesnt work.
GRANT ALL PRIVILEGES ON database_name.* TO 'root'#'localhost';
I can't recall if the mysql daemon should or should not be restarted.
I hope it helps!

Laravel Nova + Homestead SQLSTATE[HY000] [2002] Connection refused

I have a fresh installation of Homestead 7.17.0, Laravel 5.7.2 and Laravel Nova 1.0.14 but when I try to login with a username and password I added to the user's table, I get the following error:
Illuminate\Database\QueryException SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users` where `email` = user#email.com limit 1)
I'm sure the database configuration in my .env file is correct. Actually, I added the user via a and artisan command I've created and which prove the main app can connect to the database without problems.
Double check your port, when I used Homestead on my Mac the port was 3306 for running artisan commands but for loading the web application in a browser it needed to be 33060

Mysql Database connection denied

I am trying to get my Symfony 3 app running in production mode on an Amazon EC2 instance.
I am have the code in the correct place, and then I try to setup any dependencies with composer by running
export SYMFONY_ENV=prod
composer install --no-dev --optimize-autoloader
but get the error
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driverL SQLSTATE[HY000] [1044] Access denied for user 'my-user'#'127.0.0.1' to database 'my_database'
This error is then passed down to [Doctrine\DBAL\Driver\PDOException] and [PDOException] before the script terminates.
This is then followed by the message
[RuntimeException]
An error occured when executing the "'assets:install --symlink --rela'\''web'\'''" command:
which is then followed by the same errors as at the beginning.
My parameters.yml file is
parameters:
database_host: 127.0.0.1
database_port: 3306
database_name: my_database
database_user: my-user
database_password: mypassword
server_version: 5.6
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: Mysecret
registration_permitted: true
registration_limit: 3
and the user my-user has all permissions on the database my_database except GRANT.
I have been trying to solve this for a few hours to no avail, other solutions I came across involved misspelt usernames/passwords/databases (triple-checked) or needed the server version adding (which hasn't made a difference).
Additional Info
Running mysql -u my-user -p prompts for the password and then allows me to log in.
Running mysql -u my-user -p -D my_database prompts for the password and then returns ERROR 1044 (42000): Access denied for user 'my-user'#'localhost' to database 'my_database'.
Logging into mysql as root and running SHOW GRANTS FOR 'my-user'; returns ERROR 1141 (42000): There is no such grant defined for user 'my-user' on host '%'.
Logging into mysql as root and running SHOW GRANTS FOR 'my-user'#'localhost'; returns GRANT USAGE ON *.* TO 'my-user'#'localhost' IDENTIFIED BY PASSWORD hashedPassword.
Additional Info 2
After deleting my-user ('my-user'#'localhost' and 'my-user'#'127.0.0.1') and then recreating them using GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'#'localhost' IDENTIFIED BY 'password'; and also GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'#'127.0.0.1' IDENTIFIED BY 'password'; and then FLUSH PRIVILEGES; The output of SHOW GRANTS FOR 'my-user'#'localhost' is then
GRANT USAGE ON *.* TO 'my-user'#'localhost' IDENTIFIED BY PASSWORD 'passwordHash'
GRANT ALL PRIVILEGES ON 'my_database' TO 'my-user'#'localhost'
and similar output for 'my-user'#'127.0.0.1'.
But this is still giving me the same errors as before, any more ideas?
As discussed in comments you did not have any privilege on the database.
So running this fixes your problem:
GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'#'localhost'
About your doubt why you had to change it to localhost in your params because that's what you're telling mysql
Imagine you want to restrict database access to only a specific ip in your network you could run
GRANT ALL PRIVILEGES ON my_database.* TO 'my-user'#'192.168.1.10'
So replace 192.168.1.10 with 127.0.0.1 and you can use 127.0.0.1 in your parameters

Categories