By ludolfyn
I followed along with the Digitalocean LEMP stack tutorial and everything works perfectly. My app is running smoothly, but for some reason it can’t seem to write to my database. It reads and even updates the database without problems, but whenever I try to take any action that should write to the database it doesn’t work. It connects to the database, executes the query and returns the necessary data, but nothing has been written to the database.
I’ve followed the tutorial to the tee on Ubuntu 20.04. But then I also tried giving the MySQL user full privileges on the database, but still nothing. I’ve removed the user and added a new one, gave privileges, flushed privileges, and restarted MySQL too. Still nothing.
I thought I’d enable the MySQL error logging to see what errors pop up, but non of the tutorials I found worked.
Does anyone perhaps know what could be the issue here? And if not, is there a working/updated tutorial on Digitalocean that will help me to enable MySQL error logging so I can try and figure it out myself?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
I would recommend making sure that you’ve granted all privileges to the new user that you’ve created:
GRANT ALL ON example_database.* TO 'example_user'@'%';
If this still does not help, can you share an example of the problematic code snippet that you are using the insert data into the database?
Regards, Bobby
Here are the usual stuff to check when in such situations
The issue you’re describing—successful reads but failed writes to the database—indicates a deeper issue that may not be directly related to MySQL permissions. Here’s a step-by-step guide to diagnose and resolve the issue:
Even though you’ve already given full privileges to the user, confirm it explicitly using the following commands:
SHOW GRANTS FOR 'your_user'@'localhost';
Ensure you see something like:
GRANT ALL PRIVILEGES ON your_database.* TO 'your_user'@'localhost';
If you don’t see the necessary privileges, reapply them:
GRANT ALL PRIVILEGES ON your_database.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;
Certain SQL modes can prevent writes, such as STRICT_TRANS_TABLES. Check the current SQL mode:
SHOW VARIABLES LIKE 'sql_mode';
If STRICT_TRANS_TABLES or similar is set, it may reject writes that violate schema constraints. You can modify it temporarily for testing:
SET GLOBAL sql_mode = '';
To make it permanent, edit /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf and add:
[mysqld]
sql_mode=""
Restart MySQL:
sudo systemctl restart mysql
If your application uses transactions but does not commit them, writes will not persist. Test this by running a simple query in your application to explicitly commit a transaction:
BEGIN;
INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2');
COMMIT;
If this works, ensure your application is committing transactions where required.
To see all queries executed by your application, enable the general query log. This can help you identify the issue with the SQL queries.
SET GLOBAL general_log = 'ON';
SET GLOBAL general_log_file = '/var/log/mysql/general.log';
Edit /etc/mysql/mysql.conf.d/mysqld.cnf:
[mysqld]
general_log = 1
general_log_file = /var/log/mysql/general.log
Restart MySQL:
sudo systemctl restart mysql
Check the logs for write queries and errors:
sudo tail -f /var/log/mysql/general.log
MySQL logs errors to the error log file by default. To ensure logging is enabled, check your MySQL configuration:
sudo cat /etc/mysql/mysql.conf.d/mysqld.cnf | grep log_error
/var/log/mysql/error.log./etc/mysql/mysql.conf.d/mysqld.cnf:[mysqld]
log_error = /var/log/mysql/error.log
sudo systemctl restart mysql
sudo tail -f /var/log/mysql/error.log
Ensure your application is not silently failing. Common causes include:
Enable debug mode in your application to capture detailed error messages:
APP_DEBUG=true in .env.Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.