I can not access MySQL through PHP

0

I have the following problem, I use Ubuntu and I 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 just by using:

sudo mysql -u root

And now, when I try to do:

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

Nothing happens, and my page does not render.

Can anyone help me?

Update:

An important remark is that I can only access mysql through bach using the sudo permission, ie:

sudo mysql -u root

I can access.

    
asked by anonymous 31.07.2017 / 02:42

1 answer

0

Are you just making the connection or are you running a query too?

The code below performs the connection and checks if there was an error, in case of an error it will show the error message and end, in case of success in the connection it will show the success message and then close the connection with the base of data.

<?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);

Update

Follow the steps in order: 1 - end 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 finishes, then 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 NOVASENHA to a desired password and start MySQL

sudo /usr/local/mysql/support-files/mysql.server start
    
31.07.2017 / 02:50