EASY ANSWER: You do not need to create a new table for every user to track their status. You are using their $uid
so, you can use a single table with a column uid
to track their data. Then when you want to get their data, use $uid
such as
SELECT * FROM user_data
WHERE uid=$uid
Also, you can set a cron scheduler to restart MySQL if it crashes.
* * * * * systemctl is-active --quiet mysqld || systemctl restart mysqld
DETAILED ANSWER:
This is an important question especially for people who are using a very small VPS, say 1GB of RAM or less. If MySQL is dropping out, it may be a problem with your server configuration (Apache | nginx) or MySQL configuration. DOS attacks may cause an increased spike in system resource usages (see image). The end result is that MySQL process gets shutdown by the Kernel. For a long-term solution should look at optimizing your Apache or MySQL configurations.

There are several other discussions Stack Overflow those topics as well as the MySQL manual and Percona Blog:
MySQL Manual - How MySQL Uses Memory:
https://dev.mysql.com/doc/refman/8.0/en/memory-use.html
Percona - Best Practices for Configuring Optimal MySQL Memory Usage:
https://www.percona.com/blog/2016/05/03/best-practices-for-configuring-optimal-mysql-memory-usage/
How to Optimize MySQL Performance Using MySQLTuner:
https://www.linode.com/docs/databases/mysql/how-to-optimize-mysql-performance-using-mysqltuner/
Apache Memory Usage Configuration:
https://serverfault.com/questions/254436/apache-memory-usage-optimization
Apache Manual on Performance Tuning:
https://httpd.apache.org/docs/2.4/misc/perf-tuning.html
Tuning Apache Server:
https://www.linode.com/docs/web-servers/apache-tips-and-tricks/tuning-your-apache-server/
However, with respect to your original question, yes, you can script a temporary solution that checks if MySQL service is loaded and active and will restart MySQL if it is not loaded and active.
You did not mention what operating system you are using. That would help to give you a specific command. I will give you an example for CentOS linux.
Look at the following output of the command systemctl status mysql
. You can see at the top that the service is loaded and active.
[root@centos-mysql-demo ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-06-18 18:28:18 UTC; 924ms ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3350 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3273 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3353 (mysqld)
CGroup: /system.slice/mysqld.service
└─3353 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Jun 18 18:28:11 centos-mysql-demo systemd[1]: Starting MySQL Server...
Jun 18 18:28:18 centos-mysql-demo systemd[1]: Started MySQL Server.
If the service is not loaded, then a command such as:
systemctl status mysqld || systemctl restart mysqld
will do the trick of re-starting the process. You could cron that:
* * * * * systemctl status mysqld || systemctl restart mysqld
However, in the case that mysql is loaded, but the service is not active, your cron will do nothing. So, you should use a more detailed command such as:
* * * * * systemctl is-active --quiet mysqld || systemctl restart mysqld
In this case, if the service is loaded but inactive such as the state that a DOS attack can leave your mysql service, the command will also restart mysql. Using the --quiet
flag just specifies the command only to return a status code, not output anything to the screen. If you ommit the --quiet
flag you will see a status output of either active
or inactive
.
You may also create some swap space to add more available RAM resources to your server such as:
sudo dd if=/dev/zero of=/swapfile count=2096 bs=1MiB
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show
swapon --summary
free -h