By using Unix sockets instead of TCP/IP, you eliminate the overhead of establishing a network connection, resulting in improved performance. However, keep in mind that this approach only works for local connections, and you cannot use it for remote connections to your #MySQL server.
1. Determine the path of the MySQL Unix socket file. You can find it in my.cnf configuration file or by running the following command in the MySQL client: SHOW VARIABLES WHERE Variable_name = ‘socket’;
2. In Laravel, you can configure the MySQL connection in the config/database.php file. Find the ‘unix_socket’ option under the ‘MySQL’ connection and set it to the path of the MySQL Unix socket file. For example:
'mysql' => [ ... 'unix_socket' => '/path/to/mysql.sock', ... ],
After making this change, you can use the DB facade or the mysql connection in your Eloquent models to connect to your database using the Unix socket file.
In Laravel, you can also specify the socket in the DATABASE_URL environment variable, if you are using that to connect to the database. For example:
DATABASE_URL=mysql://user:password@unix(/path/to/mysql.sock)/database
3. In your PHP script, change the host parameter in the mysqli_connect() function to use the Unix socket file path instead of the hostname or IP address of your server. For example:
mysqli_connect('/path/to/mysql.sock', 'username', 'password', 'database');
4. Restart the web server to apply the changes.