Score:0

Mysqli access denied , using UNIX socket

in flag
Sam

I am trying to learn PHP, and I am setting up the database connection.
On Mysql Workbench, I created a database called php, and created a table. Then I created the account "sam"@"localhost",(I am using Ubuntu desktop, and sam is the output of whoami ) with auth_socket, Granted ALL on . , and when I press ctrl+alt+T and input mysql and press enter , I can successfully login.
Now I followed https://www.php.net/manual/en/mysqli.quickstart.connections.php , and tried using socket, I failed.

2021/08/13 16:54:35 [error] 1363#1363: *11 FastCGI sent in stderr: "PHP message: PHP Warning:  mysqli::__construct(): (HY000/1698): Access denied for user 'sam'@'localhost' in /var/www/php/my2.php on line 3PHP message: PHP Warning:  main(): Couldn't fetch mysqli in /var/www/php/my2.php on line 5" while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /my2.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "localhost:803"

And here is my code. <?php

$mysqli = new mysqli("localhost", "sam", Null, "php");

echo $mysqli->host_info . "\n";

The output is nothing in a browser.

Then I tried this.

<html>
   <head>
      <title>Connecting MySQL Server</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'sam';
         //$dbpass = 'root@123';
         $mysqli = new mysqli($dbhost, $dbuser,NULL,NULL,NULL,"/run/mysqld/mysqld.sock");
         
         if($mysqli->connect_errno ) {
            printf("Connect failed: %s<br />", $mysqli->connect_error);
            exit();
         }
         printf('Connected successfully.<br />');
         $mysqli->close();
      ?>
   </body>
</html>

The output is Connect failed: Access denied for user 'sam'@'localhost'
With log

2021/08/13 16:59:17 [error] 1363#1363: *13 FastCGI sent in stderr: "PHP message: PHP Warning:  mysqli::__construct(): (HY000/1698): Access denied for user 'sam'@'localhost' in /var/www/php/mysqli.php on line 10" while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /mysqli.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "localhost:803"

I don't have password for user sam on mysql.

MariaDB [mysql]> select user,password from user;
+-----------+-------------------------------------------+
| user      | password                                  |
+-----------+-------------------------------------------+
| root      |                                           |
| someone   | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| someone   | *9B8A3238012965AC630589D859535EA0B7C231A7 |
| someone   | *AF411B6C73B3AC3A2316A309A71758175CC14FEE |
| someone   | *D76A454D84260E84578F09915624F275F3D0E08B |
| sam       |                                           |
+-----------+-------------------------------------------+
6 rows in set (0.001 sec)

I changed the actrual username to someone to protect privacy. You can see that sam have no password.

Score:0
in flag

Use your Mysql Workbench to set a password for the user sam and use that password for connecting with the mysqli library. I don't know if Mysql Workbench does have a click-through option to do so, if not or if you prefer the SQL way, use something like:

CREATE USER 'sam'@'localhost' IDENTIFIED BY 'choose_a_safe_password'
Sam avatar
in flag
Sam
What if I want to use password-less? I learnt that more passwords means more risk. Besides, the password will be in plain_text in php. Is there an option to omit password in my php?
xogoxec344 avatar
in flag
I don't know about mysql auth without password. Could you explain why you think "more passwords mean more risk"? Where did you learn that? I strongly disagree.
xogoxec344 avatar
in flag
have a look at https://www.percona.com/blog/2019/11/01/use-mysql-without-a-password/ -> use `IDENTIFIED VIA unix_socket;` instead of a password and a linux user with the same name can connect without specifying a password.
Sam avatar
in flag
Sam
You use ssh instead of a password, and ssh keys are prior to passphrases.
xogoxec344 avatar
in flag
how you secure your ssh connection is completely independent of how you connect to mysql from a php script running on the same machine.
Sam avatar
in flag
Sam
Thanks, I see now. Maybe I should put my brain into the microwave. Haha
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.