I am unable to access Mysql from PHP

Asked

Viewed 793 times

0

I have the following problem, I use Ubunto and installed mariadb, and when I try to access mysql with:

mysql -u root

The following error appears:

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

And then I can access it using:

sudo mysql -u root

And now, when I try to do:

$conexao = mysqli_connect('localhost', 'root', '', 'php_alura');

Nothing happens, and my page doesn’t even render.

Can someone help me?

Updating:

One observing important is that I can only access mysql by Bach using the permission sudo, that is to say with:

sudo mysql -u root

I can get access.

  • What code are you using to access and display the data? What page code does not render?

  • So buddy, the connection code is as follows:: $conexao = mysqli_connect('localhost', 'root', '', 'php_alura'); However I gave a Restart in apache and is now rendering the pages, but I can not connect to mysql. OBS: I can only move mysql through the panel using the permission of sudo, I believe you have some relationship, but I don’t know how to solve.

1 answer

0


Are you just connecting or are running a query as well ?

The code below executes the connection and makes the check if there was error, in case of error will show the error message and finish, in case of success in the connection will show the success message and then close the connection with the database.

<?php
$link = mysqli_connect("HOST", "USUARIO", "SENHA", "BASE");
if (!$link) {
    echo "Error: Falha ao conectar-se com o banco de dados MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
echo "Sucesso: Sucesso ao conectar-se com a base de dados MySQL." . PHP_EOL;
mysqli_close($link);

Updating

Follow the steps in the order: 1 - finish mysql:

sudo /usr/local/mysql/support-files/mysql.server stop

2 - Start it in safe mode:

sudo mysqld_safe --skip-grant-tables

3 - This will be a continuous command until the process is over, so open another shell/ terminal window and log in without a password as root:

mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NOVASENHA';

Change NOVAPASSWORD for a desired password and start Mysql

sudo /usr/local/mysql/support-files/mysql.server start
  • Returned to me: Error: Failed to connect to Mysql database. Debugging Errno: 1698 Debugging error: Access denied for user 'root'@'localhost'

  • So friend, I tried to do the command: sudo /usr/local/mysql/support-files/mysql.server stop Porem returned the following sudo: /usr/local/mysql/support-files/mysql.server: comando não encontrado So I searched and tried to do the following: ;sudo /etc/init.d/mysql stop Which returned the following: ;[ ok ] Stopping mysql (via systemctl): mysql.service. So I tried to do the second command you gave me and returned this: 170801 13:26:23 mysqld_safe Logging to syslog.&#xA;170801 13:26:23 mysqld_safe A mysqld process already exists

  • Since you can access Mysql from the terminal using sudo, follow the steps of this print : http://imageshack.com/a/img922/2082/60pALx.png

  • Then just configure in php to connect without password

  • That should do it, right? http://imageshack.com/a/img923/7910/EmVqKQ.png

  • But your code still appears: Error: Falha ao conectar-se com o banco de dados MySQL. Debugging errno: 1698 Debugging error: Access denied for user 'root'@'localhost'

  • Yes, you have to change the values, HOST, USER, PASSWORD and BASE so $link = mysqli_connect("localhost", "root", "", "php_alura");

  • but it’s the same $link = mysqli_connect("localhost", "root", "", "php_alura");

  • Now it gets a little hard to help because I’m at work, here I can’t see the print you put in the comment, if anyone can help. if not just early tomorrow when I get home

  • All right, man, but the print was just me showing that I did exactly what you asked | root | mysql_native_password | (table in mysql)

  • This, is to stay this way the same print. Try to reinstall mysql by removing it first completely

  • Thank you very much friend!!! I had not restarted the computer and now that I tested what you asked worked!!!!! (I did not need to re install) Thank you very much!!!!

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.