shams Posted June 5, 2019 Share Posted June 5, 2019 In ubuntu 19.04 with php version 7.2 , i want to add mysql user with php when i run the code below get the error: SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' I didn't set any password for root , this is php code: <?php $server = "localhost"; $dbuser = "root"; $dbpassword = ""; try { $connection = new PDO("mysql:host=$server", $dbuser, $dbpassword); $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $user = 'test' . '@' . 'localhost'; $sqlQuery = "CREATE USER . $user . "; $connection->exec($sqlQuery); echo "User created successfully!"; } catch(PDOException $e){ echo $sqlQuery . "<br>" . $e->getMessage(); } $connection = null; ?> Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 5, 2019 Share Posted June 5, 2019 This is really a MySQL question and should be moved to the MySQL forum. You need to make sure your 'GRANT's are correct. From a CLI connection run "SHOW GRANTS FOR 'root'@'localhost'". Quote Link to comment Share on other sites More sharing options...
shams Posted June 5, 2019 Author Share Posted June 5, 2019 (edited) +---------------------------------------------------------------------+ | Grants for root@localhost | +---------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION | +---------------------------------------------------------------------+ 2 rows in set (0.00 sec) from the terminal can add the user to mysql as root. Edited June 5, 2019 by shams Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 5, 2019 Share Posted June 5, 2019 I would expect a different error but you are missing the 'dbname=...' parameter in the connect. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 5, 2019 Share Posted June 5, 2019 55 minutes ago, gw1500se said: but you are missing the 'dbname=...' parameter in the connect. Connections are to a server - specifying a default database is optional. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 5, 2019 Share Posted June 5, 2019 Agree but perhaps the default is missing. That the OP can do this with CLI, implies that the PHP script is not accessing the same host as the CLI. That seems unlikely so I am clutching at straws. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.