How to Access MySQL Error Logs
Oftentimes, the root cause of slowdowns, crashes, or other unexpected behavior in MySQL can be determined by analyzing its error logs. On Ubuntu systems, the default location for the MySQL is /var/log/mysql/error.log. In many cases, the error logs are most easily read with the less program, a command line utility that allows you to view files but not edit them:
If MySQL isn’t behaving as expected, you can obtain more information about the source of the trouble by running this command and diagnosing the error based on the log’s contents.

How To Troubleshoot MySQL Queries
Sometimes users run into problems once they begin issuing queries on their data. In some database systems, including MySQL, query statements in must end in a semicolon (;) for the query to complete, as in the following example:
If you fail to include a semicolon at the end of your query, the prompt will continue on a new line until you complete the query by entering a semicolon and pressing ENTER
Some users may find that their queries are exceedingly slow. One way to find which query statement is the cause of a slowdown is to enable and view MySQL’s slow query log. To do this, open your mysqld.cnf file, which is used to configure options for the MySQL server. This file is typically stored within the /etc/mysql/mysql.conf.d/ directory:
Scroll through the file until you see the following lines:
These commented-out directives provide MySQL’s default configuration options for the slow query log. Specifically, here’s what each of them do:
slow-query-log
: Setting this to 1 enables the slow query log.slow-query-log-file
:This defines the file where MySQL will log any slow queries. In this case,it points to the/var/log/mysql-slow.log file.long_query_time
: By setting this directive to 2, it configures MySQL to log any queries that take longer than 2 seconds to complete.log_queries_not_using_indexes
: This tells MySQL to also log any queries that run without indexes to the /var/log/mysql-slow.log file. This setting isn’t required for the slow query log to function, but it can be helpful for spotting inefficient queries.Uncomment each of these lines by removing the leading pound signs (#). The section will now look like this:
Note:If you’re running MySQL 8+, these commented lines will not be in the mysqld.cnf file by default. In this case, add the following lines to the bottom of the file:
After enabling the slow query log, save and close the file. Then restart the MySQL service:
With these settings in place, you can find problematic query statements by viewing the slow query log. You can do so with less, like this:
How To Allow Remote Access to MySQL
Many websites and applications start off with their web server and database backend hosted on the same machine. With time, though, a setup like this can become cumbersome and difficult to scale. A common solution is to separate these functions by setting up a remote database, allowing the server and database to grow at their own pace on their own machines
One of the more common problems that users run into when trying to set up a remote MySQL database is that their MySQL instance is only configured to listen for local connections. This is MySQL’s default setting, but it won’t work for a remote database setup since MySQL must be able to listen for an external IP address where the server can be reached. To enable this, open up your
mysqld.cnf file:Navigate to the line that begins with the bind-address directive. It will look like this:
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
. . .By default, this value is set to 127.0.0.1, meaning that the server will only look for local connections. You will need to change this directive to reference an external IP address. For the purposes of troubleshooting, you could set this directive to a wildcard IP address, either *, ::, or 0.0.0.0:
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
. . .Note: certain versions of MySQL the bind-address directive may not be in the mysqld.cnf file by default. In this case, add the following highlighted line to the bottom of the file:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysqllog-error = /var/log/mysql/error.log
bind-address = 0.0.0.0After changing this line, save and close the file (CTRL + X, Y, then ENTER if you edited it with nano). Then restart the MySQL service to put the changes you made to mysqld.cnf into effect:
If you have an existing MySQL user account which you plan to use to connect to the database from your remote host, you’ll need to reconfigure that account to connect from the remote server instead of localhost. To do so, open up the MySQL client as your root MySQL user or with another privileged user account
If you’ve enabled password authentication for root, you will need to use the following command to access the MySQL shell instead:
To change a user’s host, you can use MySQL’s RENAME USER command. Run the following command, making sure to change sammy to the name of your MySQL user account and remote_server_ip to your remote server’s IP address:
Alternatively, you can create a new user account that will only connect from the remote host with the following command:
Then grant the new user the appropriate privileges for your particular needs. The following example grants a user global privileges to CREATE, ALTER, and DROP databases, tables, and users, as well as the power to INSERT, UPDATE, and DELETE data from any table on the server. It also grants the user the ability to query data with SELECT, create foreign keys with the REFERENCES keyword, and perform FLUSH operations with the RELOAD privilege. However, you should only grant users the permissions they need, so feel free to adjust your own user’s privileges as necessary.
Following this, it’s good practice to run the FLUSH PRIVILEGES command. This will free up any memory that the server cached as a result of the preceding CREATE USER and GRANT statements:
Then you can exit the MySQL client:
Lastly, assuming you’ve configured a firewall on your database server, you will also need to open port 3306 — MySQL’s default port — to allow traffic to MySQL.
If you only plan to access the database server from one specific machine, you can grant that machine exclusive permission to connect to the database remotely with the following command. Make sure to replace remote_IP_address with the actual IP address of the machine you plan to connect with.
If you need to access the database from other machines in the future, you can grant them access on an ad hoc basis with this command. Just remember to include their respective IP addresses.
Alternatively, you can allow connections to your MySQL database from any IP address with the following command:
Warning: This command will enable anyone to access your MySQL database. Do not run it if your database holds any sensitive data.
Following this, try accessing your database remotely from another machine:
Note:f you added a firewall rule to only allow connections from a specific IP address, you must try to access the database with the machine associated with that address.
How To Address Crashes in MySQL
The most common cause of crashes in MySQL is that it stopped or failed to start due to insufficient memory. To check this, you will need to review the MySQL error log after a crash.
First, attempt to start the MySQL server by typing:
Then review the error logs to see what’s causing MySQL to crash. You can use less to review your logs, one page at a time:
Some common messages that would indicate an insufficient amount of memory are Out of memory or mmap can't allocate.
Potential solutions to an inadequate amount of memory are:
How To Fix Corrupted Tables in MySQL
Introduction
Occasionally, MySQL tables can become corrupted, meaning that an error has occurred and the data held within them is unreadable. Attempts to read from a corrupted table will usually lead to the server crashing.
Some common causes of corrupted tables are:
If you suspect that one of your tables has been corrupted, you should make a backup of your data directory before troubleshooting or attempting to fix the table. This will help to minimize the risk of data loss.
First, stop the MySQL service:
Note: On some platforms such as Rocky Linux, the MySQL service may be called mysqld instead.
Then copy all of your data into a new backup directory. On Ubuntu systems, the default data directory is /var/lib/mysql/:
After making the backup, you’re ready to begin investigating whether the table is in fact corrupted. If the table uses the MyISAM storage engine, you can check whether it’s corrupted by restarting MySQL and running a CHECK TABLE statement from the MySQL prompt:
A message will appear in this statement’s output letting you know whether or not it’s corrupted. If the MyISAM table is indeed corrupted, it can usually be repaired by issuing a REPAIR TABLE statement:
Assuming the repair was successful, you will see a message like this in your output:
On the other hand, if the corrupted table uses the InnoDB storage engine, then the process for repairing it will be different. InnoDB is the default storage engine in MySQL as of version 8.0, and it features automated corruption checking and repair operations. InnoDB checks for corrupted pages by performing checksums on every page it reads, and if it finds a checksum discrepancy it will automatically stop the MySQL server.
There is rarely a need to repair InnoDB tables, as InnoDB features a crash recovery mechanism that can resolve most issues when the server is restarted. However, if you do encounter a situation where you need to rebuild a corrupted InnoDB table, the MySQL documentation recommends using the “Dump and Reload” method. This involves regaining access to the corrupted table, using the mysqldump utility to create a logical backup of the table, which will retain the table structure and the data within it, and then reloading the table back into the database.
With that in mind, try restarting the MySQL service to see if doing so will allow you access to the server:
In the [mysqld] section, add the following line:
[mysqld]
. . .innodb_force_recovery=1
Save and close the file, and then try restarting the MySQL service again. If you can successfully access the corrupted table, use the mysqldump utility to dump your table data to a new file. You can name this file whatever you like, but here we’ll name it out.sql:
Then drop the table from the database. To avoid having to reopen the MySQL prompt, you can use the following syntax:
Following this, restore the table with the dump file you just created:
How To Troubleshoot Socket Errors in MySQL
MySQL manages connections to the database server through the use of a socket file, a special kind of file that facilitates communications between different processes. The MySQL server’s socket file is named mysqld.sock and on Ubuntu systems it’s usually stored in the /var/run/mysqld/ directory. This file is created by the MySQL service automatically.
Sometimes, changes to your system or your MySQL configuration can result in MySQL being unable to read the socket file, preventing you from gaining access to your databases. The most common socket error looks like this:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
One common cause of this error is that the MySQL service is stopped or did not start to begin with, meaning that it was unable to create the socket file in the first place. To find out if this is the reason you’re seeing this error, try starting the service with systemctl:
Then try accessing the MySQL prompt again. If you still receive the socket error, double check the location where your MySQL installation is looking for the socket file. This information can be found in the mysqld.cnf file:
Look for the socket parameter in the [mysqld] section of this file. It will look like this:
[mysqld]
user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 . . .Close this file, then ensure that the mysqld.sock file exists by running an ls command on the directory where MySQL expects to find it:
If the socket file exists, you will see it in this command’s output:
If the file does not exist, the reason may be that MySQL is trying to create it, but does not have adequate permissions to do so. You can ensure that the correct permissions are in place by changing the directory’s ownership to the mysql user and group:
Then ensure that the mysql user has the appropriate permissions over the directory. Setting these to 775 will work in most cases:
Finally, restart the MySQL service so it can attempt to create the socket file again:
Then try accessing the MySQL prompt once again. If you still encounter the socket error, there’s likely a deeper issue with your MySQL instance, in which case you should review the error log to see if it can provide any clues.