Can't connect to MySQL through MAMP - php

So here's my array containing my credentials.
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '*',
'db' => 's'
)
And the actual connection
try{
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/username'));
}
catch(PDOException $e){
die($e->getMessage());
}
And this is the error I get
SQLSTATE[28000] [1045] Access denied for user 'root'#'localhost' (using password: YES)
But I am aware that this means access is denied, but the credentials are 100% correct. I also tested via command line and through workbench.

Are you sure a password is set? What happens if you attempt to connect via the command line line this:
/Applications/MAMP/Library/bin/mysql -uroot
Also, the password for root in MAMP is root. This is no deep/dark secret. Is the * in your example valid?
And in your connector, have you tried using localhost instead of 127.0.0.1? I have seen some MySQL setups work with one but not the other when it should be both.
EDIT: If you somehow mucked up the root password, don’t panic! You can still reset it this way. Warning, some folks feel this method of password reset is “risky” but that is generally true for a production server or any server in the wild. From MAMP on your desktop, this should be 100% safe.
First, stop MAMP entirely.
Next, start it up again from the command line with the skip-grant-tables option like so:
/Applications/MAMP/Library/bin/mysqld --skip-grant-tables
Once that is done, you can login with 100% no password just like this:
/Applications/MAMP/Library/bin/mysql -uroot
Then you can reset the root password with this one-liner:
UPDATE user SET Password=PASSWORD('root') WHERE user='root'; FLUSH PRIVILEGES; exit;
Okay, now find the process running the MySQL daemon with skip-grant-tables from the command line like this:
ps -u [your system username] | grep "mysqld --skip-grant-tables"
A list with two items should be returned: One is the mysqld & the other is the command you just made. Something like this:
502 1759 ttys004 0:00.11 /Applications/MAMP/Library/bin/mysqld --skip-grant-tables
502 1766 ttys004 0:00.00 grep mysqld
Okay, so now we know the mysqld with skip-grant-tables has process ID 1759, go ahead and kill that like so:
kill 1759
Restart MAMP again & the root password should work as expected now.

Try adding a port to your array! For me, it worked! The port is 8889 by default. You can change it by going to MAMP>Preferences, then the Ports tab. Here's what I think (I haven't tried this!):
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '*',
'port' => '8889',
'db' => 's'
)

You use Config::get('mysql/username') twice, the second should be Config::get('mysql/password')

Related

Access denied for 'user'#'localhost'

I'm trying to create model using phalcon. But when I type phalcon model polls I get an error as ERROR: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES).
I can get into mysql using mysql -p. So I tried to create new user and grant him all privileges, but it didn't help. I also can login into phpmyadmin using root and password. I also tried to specify the port as 3306 in config file, but still nothing.
My config file:
defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');
return new \Phalcon\Config([
'database' => [
'adapter' => 'Mysql',
'host' => 'localhost',
'port' => 3306,
'username' => 'root',
'password' => '$Pass1234 ',
'dbname' => 'poll_db',
'charset' => 'utf8',
],
'application' => [
'appDir' => APP_PATH . '/',
'controllersDir' => APP_PATH . '/controllers/',
'modelsDir' => APP_PATH . '/models/',
'migrationsDir' => APP_PATH . '/migrations/',
'viewsDir' => APP_PATH . '/views/',
'pluginsDir' => APP_PATH . '/plugins/',
'libraryDir' => APP_PATH . '/library/',
'cacheDir' => BASE_PATH . '/cache/',
// This allows the baseUri to be understand project paths that are not in the root directory
// of the webpspace. This will break if the public/index.php entry point is moved or
// possibly if the web server rewrite rules are changed. This can also be set to a static path.
'baseUri' => preg_replace('/public([\/\\\\])index.php$/', '', $_SERVER["PHP_SELF"]),
]
]);
P.S I did all operation about granting privileges entering to mysql by mysql -u root -p
P.P.S I also did FLUSH PRIVILEGES and then restarted mysql, but nothing. Thanks for future help.
UPD: I also tried to created project in laravel, and made migration and everything works fine. I also used root + password.
SOLVED: I don't know why, but I decided to create new project, and everything worked fine even with 'root'#'localhost'. Seems like something wrong was with first project. Even so, thank for all people who participated in helping.
P.S I marked EternHour's answer as solution just for people who might get same error in future.
I don't think this is a port issue, the request is reaching it's destination. Using root#localhost will work when logging in through the command line (mysql -u root -p) but you don't want to use it for connecting with your code. Keep in mind that when establishing your connection, you need to use host=localhost or host=127.0.0.1 explicitly. If you use the IP address (even on the same server), you will get access denied.
[user#localhost ~]# mysql --host=127.0.0.1 --protocol=TCP -u root -p
Enter password:
mysql>
[user#localhost ~]# mysql --host=192.168.1.10 --protocol=TCP -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'hostname' (using password: YES)
Here are the steps I'd recommend taking:
Create a dedicated user that you can use for connecting in your scripts.
If source of script is same server as MySQL.
CREATE USER '<user>'#'localhost'
IDENTIFIED BY 'password';
GRANT ALL
ON <database>.*
TO '<user>'#'localhost';
If the connection is always established from the same place but a different location than MySQL, run the following on the command line.
CREATE USER '<user>'#'<IP_address>'
IDENTIFIED BY 'password';
GRANT ALL
ON <database>.*
TO '<user>'#'<IP_address>';
If the source of the connection varies, run the following command.
CREATE USER '<user>'#'<IP_address>'
IDENTIFIED BY 'password';
GRANT ALL
ON <database>.*
TO '<user>'#'%';
Here is a link to the documentation if you have any questions.

drush si failure user has access, Failed to create database: ERROR 1045 (28000): Access denied for user

I am running the following sets of commands to automatically install a certain drupal website
phpMyAdmin, has a database named x_init_testing, and has a user testing with password secret that can log into it (a manual drupal install is working fine, and there is an access to the db with the user and the password)
First, I am running
mysqladmin -utesting -psecret drop x_init_testing -f
mysqladmin -utesting -psecret create x_init_testing
both of these work fine, and clear the database (which should allow drush to install a new site on it)
then I am running
drush si profileName --db-url=mysql://root#127.0.0.1/x_init_testing -y --account-pass=secret
root has a password (i.e., it is not empty)
and drush sql connect does work
drush sql-connect
mysql --user=testing --password=secret --database=x_init_testing --host=127.0.0.1 --port=8889
the site is stored on MAMP (i.e., locally on my machine so no network problems)
and my settings.php file is configured with the db
*/
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'x_init_testing',
'username' => 'testing',
'password' => 'secret',
'host' => '127.0.0.1',
'port' => '8889',
'driver' => 'mysql',
'prefix' => '',
),
),
);
Still running the drush si results in
You are about to CREATE the 'x_init_testing' database. Do you want to continue? (y/n): y
Failed to create database: ERROR 1045 (28000): Access denied for user[error]
'root'#'localhost' (using password: NO)
If needed I am using: Drush Version 8.1.6, and it seems that I am using all the required arguments for the si
I think I found the issue, in theory the root user starts with no password, and this is why the command worked on machines that did not set a root password
changing the si command to :
drush si profileName --db-url=mysql://root:password#127.0.0.1/x_init_testing -y --account-pass=secret

Laravel Access denied for user 'root'#'localhost' (using password: YES) in laravel 4.2

I have old project which built using Laravel 4.2.I am getting following error
PDOException (1045)
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)
I have googled and tried every think but i couldn't able to fix it
.env file
APP_KEY=az9tq5VHQCV9g5m2CsgY89jtijrCMgEA
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
database.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'billing',
'username' => 'root',
'password' => '1234',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Can any one guide me where m doing wrong ?
Note: Before asking question i tried by updating composer update as well as most of the stackoverflow answers.
Updated
I have tested this connection by creating php file
<?php
$servername = "localhost";
$username = "root";
$password = "1234";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
i will get Connected successfully message
My error was Laravel Access denied for user 'root'#'localhost' (using password: NO)
and I only got this error at my host site.
In .env I changed:
DB_USERNAME=user
DB_PASSWORD=password
to:
DB_USERNAME='user'
DB_PASSWORD='password'
Worked for me!
DB_HOST="localhost" worked for me.
And to be safe, wrap .env variables in double quotes to avoid ENV errors:
DB_USERNAME="root"
DB_PASSWORD="#Password"
These will give errors: DB_PASSWORD=#Password, DB_PASSWORD=I have spaces
# is a start of a comment and is exactly the same as //
Run php artisan serve after configure .env, not before.
Laravel 4.x doesn't even support ENV files. You just have to see whether settings in ./config/[env name]/database.php are correct.
I just ran into the same problem on a new Laravel install.
The problem was that I was connecting to my ubuntu localhost mysql server instead of to the vagrant box's mysql server that was on it's own ip address '192.168.10.10'.
I changed over to that and all worked a charm :)
I'm test in Laravel 5.6.33 and i see this error: "Access denied for user 'root'#'localhost'", but the username and password is correct.
In this version i think of this is a BUG, i change DB_HOST, from 127.0.0.1 to localhost and works!
DB_HOST=127.0.0.1 (before)
DB_HOST=localhost (after)
Try In .ENV
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
And Delete Cache Files From Root/boostrap/chache/files
and Run The App
The error says it all, Laravel can't connect to a DB. Check priveleges and make sure the DB exists. Also, try to connect to a DB with a client, like MySQL Workbench using same login and password. This will give a hint about what you can do to fix this. If you can't do this, it's not a Laravel issue.
Try This
.env file
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
config/database.php
default' => env('DB_CONNECTION', 'mysql'),
'mysql' => array(
'driver' => 'mysql',
'host' => env('DB_HOST','localhost'),
'database' => env('DB_DATABASE','billing'),
'username' => env('DB_USERNAME','root'),
'password' => env('DB_PASSWORD', '1234'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
Else checkout the .env file and check the connection
I got the same problem. You should add "" to the DB_USERNAME and DB_PASSWORD
So,
Step 1: DB_USERNAME="root" DB_PASSWORD="1234"
Step 2: Run "php artisan config:clear"
Step 3: Run "php artisan cache:clear"
please delete config file located at bootstrap/cache/config.php
it will be autogenerated when you run php artisan config:cache command
it will work
I faced the same problem when learning laravel using homestead, the problem happened when I try to open a page. It tried to connect to database but got rejected, although I can connect to the database using mysql command line.
The problem was I had wrong assumption that since homestead forward web server request from guest to host, the same thing is also same with myqsl, but apparently it is not.
Homestead has it's own mysql service, so my problem was fixed by:
- homestead ssh into the guest machine
- mysql -u root -p to connect to mysql command line, default password is "secret"
- create the database, but you need to change your password first, just change into the same password as in mysql root's password in your host machine for convenience
- exit the mysql cli and
- php artisan migrate to create the tables
- refresh your browser, now it should be able to connect to your db
Access denied for user 'root'#'localhost' (using password: YES)
The error was coming? The possible reason is your database
connection.If the database is not connect then throw the error.
So Check the database releted all things and If all things is correct.
If not solved then change
DB_HOST=127.0.0.1 TO DB_HOST=localhost
Make sure your .env is using the correct port number. If you're using phpMyAdmin this is how is setup:
MariaDB defaults to 3306
and
MySQL defaults to 3308
So change your .env to use port 3308 for MySQL. I hope this helps!
I changed this line DB_HOST=127.0.0.1 in .env to DB_HOST=localhost and it worked for me.
I had a similar problem, the password is the major issue in such communication, see my picture below, i solved it after changing password, php artisan serve and then reloading my view. It worked.
please login to mysql or phpmyadmin and change your password.
enter image description here
It sounds kind of obvious, but check the credentials of the database.
For me something was wrong in the env file. So I created a fresh installation, copied the env file from there and updated the credentials and it worked.
If you're using docker and still having issues with user access denied issues, you may want to try starting over fresh with the DB, which is was what it took for me.
First, I figured out where my DB data volumes were being stored in my docker-compose.yml file, which ended up being here: [app root dir]/docker/db/data/
db:
container_name: ${APP_NAME}_db
image: mysql:8.0
ports:
- 33060:3306
volumes:
- ./docker/db/data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
- MYSQL_DATABASE=${DB_DATABASE}
Then I ran this in the app root directory to remove all the docker containers.
docker-compose rm -v
Next I navigated to the DB data files and removed all of them:
cd ~/[app root dir]/API/docker/db/data
sudo rm -rf ./*
Finally, I started up the containers and was then able to connect with my database user credentials.
docker-compose up --build --force-recreate --no-deps
REFERENCE: https://github.com/docker-library/mysql/issues/51
If the username and password are correct and the problem still persists, try clearing the cache using the following method
Route::get('/clear-cache', function () {
Artisan::call('cache:clear');
Artisan::call('route:clear');`
});
Laravel 6 just set secret instead of empty space.
DB_PASSWORD=secret

Connection Timeout on Openshift MySQL Cartgridge

I'm deploying a PHP app which uses Doctrine as ORM in Openshift. In my post_deploy action hook I run doctrine orm schema-tool to update the DB but I'm getting a Connection Timeout error.
As I'm farily new to Openshift, what is the best way to setup the database on Openshift?
UPDATE
I have two gears, one for the app and other for MySQL. I ssh into the app gear and ran
doctrine orm:schema-tool:update
Which results in a Connection Timeout error.
If I try
mysql -h $OPENSHIFT_MYSQL_DB_HOST
-p $OPENSHIFT_MYSQL_DB_PORT
-u $OPENSHIFT_MYSQL_DB_USERNAME
-P $OPENSHIFT_MYSQL_DB_PASSWORD
-
I get a prompt and the subsequent error after entering the MySQL password informed by Openshift.
Enter password:
ERROR 1049 (42000): Unknown database '<obsfucated_password>'
UDPATE 2:
As an unrelated thing pointed by Martin below, the mysql syntax is wrong. After fixing it I found that
mysql -u $OPENSHIFT_MYSQL_DB_USERNAME
-h $OPENSHIFT_MYSQL_DB_HOST
-P $OPENSHIFT_MYSQL_DB_PORT
-D $OPENSHIFT_APP_NAME
-p $OPENSHIFT_MYSQL_DB_PASSWORD
Results in
Enter password:
ERROR 1045 (28000): Access denied for user 'adminjbEnwMq'#'10.63.71.68' (using password: NO)
While runnning
mysql -u $OPENSHIFT_MYSQL_DB_USERNAME
-h $OPENSHIFT_MYSQL_DB_HOST
-P $OPENSHIFT_MYSQL_DB_PORT
-D $OPENSHIFT_APP_NAME
Connects successfully.
UPDATE 3
On the PHP side I'm running the following test code, thanks to this answer:
<?php
require_once "vendor/autoload.php";
require_once "vendor/doctrine/common/lib/Doctrine/Common/ClassLoader.php";
use Doctrine\DBAL\Configuration;
$config = new \Doctrine\DBAL\Configuration();
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT',getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER',getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS',getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME',getenv('OPENSHIFT_GEAR_NAME'));
$connectionParams = array(
'dbname' => DB_NAME,
'user' => DB_USER,
'password' => DB_PASS,
'host' => DB_HOST,
'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
var_dump($conn);
echo $conn->query("SHOW TABLES");
?>
And I get the same timeout error.
I cannot help you with your doctrine question, but the mysql command syntax is actually wrong.
Usage: mysql [OPTIONS] [database]
As you can see in your output, mysql is interpreting your password as the database name.
If you want to specify the optionas as you showed above you need to add an '=' between the option and the option-value.
mysql --host=$OPENSHIFT_MYSQL_DB_HOST
--port=$OPENSHIFT_MYSQL_DB_PORT
--user=$OPENSHIFT_MYSQL_DB_USERNAME
--password=$OPENSHIFT_MYSQL_DB_PASSWORD
If you want to add the database name to your command, the default database name is $OPENSHIFT_APP_NAME. Just add it at the end of your mysql command.
To see all the mysql options do mysql --help
Maybe it was because I was always working on this after 23hs, but this morning I realized I was not setting the port connection parameter. The following code works like a charm:
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => getenv('OPENSHIFT_MYSQL_DB_HOST')?: '127.0.0.1',
'port' => getenv('OPENSHIFT_MYSQL_DB_PORT')?:'3306',
'user' => getenv('OPENSHIFT_MYSQL_DB_USERNAME')?: 'stpe',
'password' => getenv('OPENSHIFT_MYSQL_DB_PASSWORD')?: 'thefallen1',
'dbname' => getenv('OPENSHIFT_APP_NAME')?:'stpe',
);

Cannot connect to PgSQL via Laravel SQLSTATE[08006] [7] FATAL

I'm trying to connect to pgsql via laravel and finally got everything setup (pgsql server running, pdo installed, all libs installed). I'm running on a VPS (CentOS) managed via CPanel/WHM.
Here's what I'm doing:
I'm trying to create a user database via artisan (Laravel's command line) using migrate:install.
For those that don't use Laravel, artisan uses php's PDO for pgsql to connect. Here are my settings:
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
),
I've also setup a test.php file:
$dbconn = pg_connect("host=localhost port=5432 dbname=dbname user=username password=password");
which also fails. I used phpPgAdmin to see what's up and all of the permissions are set correctly, the database shows up, the username is correct, same with password. I checked where postgre (version 8.4.12 btw) was running and phpPgAdmin tells me "localhost:5432".
The error I get via the command line is the following:
SQLSTATE[08006] [7] FATAL: no pg_hba.conf entry for host "::1", user "myusername", database "my_database", SSL OFF
Now, I tried to find the pg_hba.conf file but I'm not entirely sure where to look for it. Same goes for the error/access logs for pg and google hasn't been any help as far as this goes.
Any idea on what I can do?
localhost points to IPV6 ::1 address on your system. Postgresql makes the difference between ipv6 and ipv4 addresses when dealing with access list.
I was able to install/configure everything correctly. I changed the "host" to 127.0.0.1, not sure why that made a difference but it did :)

Categories