While setting up a Laravel project and running php artisan serve
, I encountered the following error: Error: Class “DOMDocument” not found.
See if you tried all the ways but sitll not able to configure the setup, better to use cloud solution. Try https://idx.google.com/ in this case. There is just one click Laravel setup with your Google id.
Additionally, when trying to run database migrations with php artisan migrate
, I faced a MySQL connection issue:
SQLSTATE[HY000] [2002] Connection refused
Further debugging with mysql -h 127.0.0.1 -P 3306 -u root -p resulted in:
ERROR 2003 (HY000): Can't connect to MySQL server
Even when attempting to start MySQL with sudo systemctl start mysql, it failed with:
Failed to start mysql.service: Unit mysql.service not found.
Lastly, I faced an authentication issue when trying to log in as root:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
The Solution
1. Fixing the DOMDocument Not Found Error
This happens because the php-xml extension is missing. The fix:
sudo apt install php-xml
Restart the server: php artisan serve
2. Resolving MySQL Connection Issues
✅ Verify MySQL is Installed
If MySQL isn't installed, install it with:
sudo apt update
sudo apt install mysql-server
✅ Start and Enable MySQL
Start the MySQL service and ensure it runs on system startup:
sudo systemctl start mysql
sudo systemctl enable mysql
✅ Check MySQL Status
Verify if MySQL is running:
sudo systemctl status mysql
If it's not running, try restarting it: sudo systemctl restart mysql
✅ Fixing "Access Denied for User 'root'@'localhost'"
If MySQL denies access to root, switch to the MySQL shell using sudo:
sudo mysql -u root
Then, update the root user authentication method:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';
FLUSH PRIVILEGES;
EXIT;
Now, try logging in again:mysql -u root -p
✅ Updating Laravel's .env Database Configuration
Ensure your Laravel .env file has the correct database settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=root
DB_PASSWORD=your_new_password
Then, clear the Laravel config cache:
php artisan config:clear
php artisan config:cache
3. Running Migrations Successfully
Once the database connection is fixed, run:php artisan migrate
Conclusion
By installing php-xml, ensuring MySQL is installed and running, and updating authentication methods, I resolved both Laravel and MySQL errors. If you face similar issues, these steps should help you get back on track quickly! 🚀