https://bugs.documentfoundation.org/show_bug.cgi?id=136290

Mysql Gyuder <[email protected]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Whiteboard|                            |tutorial on installing
                   |                            |MySQL on Ubuntu 20.04
                URL|                            |https://docs.vultr.com/how-
                   |                            |to-install-mysql-on-ubuntu-
                   |                            |24-04

--- Comment #5 from Mysql Gyuder <[email protected]> ---
I faced a similar issue when setting up MySQL on Ubuntu 20.04 and integrating
it with PHP. The problem occurred due to MySQL's default `auth_socket` plugin
for authentication, which often causes connection issues when using PHP. Here's
how I resolved it, step-by-step:

---

### Step 1: Verify MySQL Installation  
Ensure that MySQL is installed and running on your Ubuntu system. You can
confirm by checking the service status:  
```bash
sudo systemctl status mysql
```  
If MySQL isn’t installed, follow this comprehensive [tutorial on installing
MySQL on Ubuntu
20.04](https://docs.vultr.com/how-to-install-mysql-on-ubuntu-24-04).

---

### Step 2: Identify the Unix Socket File  
Ubuntu places the MySQL socket file in `/var/run/mysqld/mysqld.sock` by
default. You can confirm the location in the MySQL configuration file:  
```bash
sudo cat /etc/mysql/mysql.conf.d/mysqld.cnf
```  
Look for the line:  
```text
socket = /var/run/mysqld/mysqld.sock
```  

---

### Step 3: Adjust Root Authentication  
MySQL on Ubuntu 20.04 often uses the `auth_socket` plugin for the root user.
This means you can connect without a password from the terminal but not from
PHP. To fix this:  

1. Log into MySQL:  
   ```bash
   sudo mysql
   ```  

2. Change the authentication method for the root user:  
   ```sql
   ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY
'your_password_here';
   FLUSH PRIVILEGES;
   ```  

3. Test the new credentials by running:  
   ```bash
   mysql -u root -p
   ```  
   Enter the password you just set.

---

### Step 4: Test PHP Connection to MySQL  
Create a PHP file (`db_test.php`) to test the connection:  
```php
<?php
$servername = "localhost";
$username = "root";
$password = "your_password_here";
$dbname = "test_db";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
```  

Place this file in your Apache web directory (usually `/var/www/html`) and open
it in your browser. If everything is set up correctly, it should display:  
```text
Connected successfully
```  

---

### Step 5: Create a Dedicated MySQL User (Optional)  
For better security, create a dedicated user for your application:  
```sql
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON test_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
```  
Update your PHP script to use this new user's credentials.

---

### Additional Resources  
I found this tutorial on MySQL setup ) very helpful for understanding MySQL
installation and configuration on Ubuntu. It also provides insights into
handling authentication methods and optimizing your setup.

By following these steps, I was able to resolve the connection issue and
properly integrate MySQL with PHP. Hopefully, this helps others facing the same
challenge!

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to