When trying to access my Laravel website I'm getting an SQL connection refused error when trying to execute $candidate = App\Models\Candidate::find(400);
SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' (SQL: select * from `candidates` where `candidates`.`candid` = 400 limit 1)
However, I can connect fine using php artisan tinker from the command line:
root#454ab4403b8f:/app# php artisan tinker
Psy Shell v0.9.6 (PHP 7.2.7-1+ubuntu18.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> App\Models\Candidate::find(400)
=> App\Models\Candidate {#2865
candid: 400,
firstname: "",
...
}
Is there a different configuration for artisan than web? Why would one be working but not the other?
The ROOT account on a MySQL database is usually blocked for login that is not console. Artisan is console and your user is recognised.
Just make in your database a dedicated database and database user for your development purposes.
And never use root for web applications. Always make a dedicated user for databases and such. Root has too much power, and some errors that would possibly pop up on a normal user won't pop up under root which could lead to unexpected bugs when moving to production. In your local environment you wish to mimic production as close as possible, with all the same limitations.
Also if your website would end up with a weakness and on production you also use ROOT as database user, then if a hacker found the weakness the hacker could read/modify/wipe all the data in all your databases and possibly have access to your filesystem and gather other files that way. Just never use root except to configure the database.
Related
I've deploy a laravel API some months ago into a shared hosting (banahosting). It's been working fine until yesterday that all API endpoints returns an error:
Illuminate\Database\QueryException: SQLSTATE[HY000] [1045] Access denied for user '####'#'localhost' (using password: YES) (SQL: select `destinations`.*, (select count(*) from `experiences` where `destinations`.`id` = `experiences`.`destination_id`) as `experiences_count` from `destinations` order by `id` desc) in file /###/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 669
Its weird because no-one else than me has access to the hosting, so i supposed that maybe some strange security function of this shared hosting has changed the password of the db user so i just changed it again, updated the .env file and ran the commands to clear artisan cache but the error has remained.
I've tried creating a new DB user and then i found the real problem: even when i changed the db user on the .env file, the error says that the user which is trying to access is the same. I mean, the error should say something like "Access denied for user 'NEW_USER'#'localhost'" but instead it says "Access denied for user 'OLD_USER'#'localhost'"
I also tried setting directly the user and password into the file /config/database.php or deleting completely the .env file but the error is exactly the same.
The support team of the hosting tried to help me too, they told me that they cleaned the cache of the server but it didn't work and they don't know what could be happening.
The command after every changed i've tried is php artisan config:clear and php artisan config:cache, i've also tried with php artisan config:clear and php artisan optimize
I need help here, because it's driving me crazy. I'm trying to connect to the remote production DB but I'm unable. I have read tons of posts about this that are focusing on the credentials and also running the following commands to clean config that are stored in Laravel cache:
php artisan cache:clear
php artisan config:cache
php artisan config:clear
Laravel 5 error SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
Any of these are working for me.
I have checked very well the credentials both in my .env file and my database.php. I'm able to connect to a local database by using the ip "127.0.0.1" but I'm unable with the production DB stored in Google Cloud. It's good to mention that my local IP is allowed to access that Database and I'm able to connect by using MySql Workbench.
Here is what I see when I perform "php artisan tinker" and then "DB::connection()->getPdo();" to check my connection from terminal:
The only thing that calls my attention is the message containing "so#localhost" when I'm not performing a connection to localhost but I don't know if that's related with the issue.
Any other suggestion? I have lost too much time trying to find out what could be happening.
EDIT 1: Laravel Framework 5.8.38
After two days fighting with this one, I readed tons of posts and a lot of unuseful solutions I have found my solution by looking at this video.
Basically, you must stop you XAMPP MySQL Server and open C:\xampp\mysql\bin\my.ini and edit by adding the following command "skip-grant-tables" after the [mysqld] attribute. The result in this file might look this way:
# The MySQL server
default-character-set=utf8mb4
[mysqld]
skip-grant-tables
socket="C:/xampp/mysql/mysql.sock"
...
After that, I ran "php artisan tinker" and DB::connection()->getPdo(); and everything was connected again, but it's good if you first clean your cache and config.
As mentioned by others (Ref 1,
Ref 2 and Ref 3) the current phpMyAdmin version (4.8.2) + MySQL 8.0.11 + PHP 7.2.8 doesn't work as expected.
The error happens when you try to open phpMyAdmin, where it shows an error message:
#2054 - The server requested authentication method unknown to the client
mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client
As stated in MySQL migration manual here, and described as the solution in Ref 2, you can correct this by executing the following mysql query inside docker:
ALTER USER root IDENTIFIED WITH mysql_native_password BY 'ROOTPASSWORD';
(In order to run this, I use docker exec -it CONTAINERID bash and execute mysql command)
And it works. The main problem is that I need to fire this query as soon as the container starts (the current method is manual and far from scalable). I assume it's possible in different ways:
By executing a PHP script that runs the query after everything has been setup (IMO this is unsafe )
By running the above command in a bash after everything has been setup
The first one is unsafe IMO as the mysql user must have high privileges, although there will be only the root user when this script runs. I haven't tested it yet.
The seconde one is the best IMO, but I can't figure it out how to implement using Dockerfile, as everything I type there runs BEFORE mysql and phpMyAdmin are installed. How could I achieve that?
My current Dockerfile can be seen here and the docker-compose.yml can be seen here.
As a simple solution, you can build your custom mysql image and run the command there. Then mysql will start with this modification already in place.
I am working on a project using the Laravel 4.2 framework. I want to execute the command php artisan migrate but when I run this command it shows an error:
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1142 CREATE command denied to user 'abc'#'10.1.1.27' for table 'migrations'
I think I have placed the project files and the database on different servers, so that's why am I getting this error.
Database server: 10.1.1.56
Project server: 10.1.1.27 (localhost)
This error indicates that the remote MySQL database isn't configured to allow certain operations executed by the user abc from the IP address 10.1.1.27. In many cases, MySQL users are set up to allow access from the the same host as the database server, but we need to explicitly GRANT access for clients connecting to the database from a remote host.
We can use the following commands to grant the user abc access from remote hosts. We must run these commands as a user that has the ability to grant privileges to other users (such as the database's root user):
GRANT ALL PRIVILEGES ON database.* TO 'abc'#'%';
FLUSH PRIVILEGES;
Replace database with the name of the application's database configured in .env or config/database.php. The wildcard '%' in 'username'#'%' indicates that we want to give permissions to that user from any IP address. If we don't want to allow the user access from any IP address, we can limit access to specific IP addresses by replacing the wildcard with the address to allow (10.1.1.27 in this case).
Depending on the needs of the project, we may not need to give a user all privileges to a database. See the documentation for a list of available privileges that we can assign.
1142 CREATE command denied to user 'abc'#'10.1.1.27' for table 'migrations'
The above command simply means that the user don't have CREATE permission on the connected database. So first of all you have to grant the privileges to that user on the database and after that run the migration.
Explanation: When you run migrate, a table is created on with the name migration in the database that maintains the status of migration ion it, and you don'r have the CREATE permission that's why it is showing error.
How would I go about doing this (the title is the question), I've changed all my ENV files to my hosted mysql details. When i run php artisan migrate from composer I get the
"denied username#localhost using password yes error".
I've changed the port and everything but I'm not sure what to do anymore and I'm 99% sure that the details are correct I've tried them multiple times
You need check somethings:
are credentials valid (username, password and host)?
the database host is accessible by your application server?
.env file is correctly configured or have some config hard coded in app/config/database.php?
To validade connection, you can try this: http://www.w3schools.com/php/php_mysql_connect.asp
About configuration and enviroments : https://laravel.com/docs/5.2/configuration