MariaDB 10 CentOS 7 moving datadir woes - mysql

Brand new "minimal" install of CentOS 7 along with MariaDB 10. I have an additional mounted mirrored volume that I want to use for the datadir. Startup sequence is fine and completes normally when my.cnf [mysqld] is commented out. I've copied the data..
sudo cp -R -p /var/lib/mysql/* /mnt/mysql/
The permissions are identical to those of the original. The volume is in /etc/fstab and mounts fine
/dev/sdb1 /mnt/mysql xfs defaults 0 0
[root#femur mysql]# ls -la
total 110632
drwxr-xr-x. 5 mysql mysql 4096 Oct 20 15:27 .
drwxr-xr-x. 3 root root 18 Oct 16 16:46 ..
-rw-rw----. 1 mysql mysql 16384 Oct 20 15:27 aria_log.00000001
-rw-rw----. 1 mysql mysql 52 Oct 20 15:27 aria_log_control
-rw-r-----. 1 mysql root 7005 Oct 20 13:49 femur.err
-rw-rw----. 1 mysql mysql 12582912 Oct 20 15:27 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Oct 20 15:27 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Oct 20 12:21 ib_logfile1
-rw-rw----. 1 mysql mysql 0 Oct 20 12:22 multi-master.info
drwx--x--x. 2 mysql mysql 4096 Oct 20 12:21 mysql
drwx------. 2 mysql mysql 4096 Oct 20 13:37 performance_schema
drwxr-xr-x. 2 mysql mysql 6 Oct 20 12:21 test
this is in my.cnf
!includedir /etc/my.cnf.d
[mysqld]
log_error = /var/log/mysql-error.log
user = mysql
datadir = /mnt/mysql
socket = /mnt/mysql/mysql.sock
This is what I get when I try to start it...
'[root#femur mysql]# sudo systemctl start mysql.service
Job for mysql.service failed. See 'systemctl status mysql.service' and 'journalctl -xn' for details.'
Neither of those two files says much, but this is in /var/log/mysql-error.log
141020 16:07:09 mysqld_safe Starting mysqld daemon with databases from /mnt/mysql
141020 16:07:09 [Warning] Can't create test file /mnt/mysql/femur.lower-test
141020 16:07:09 [Note] InnoDB: Using mutexes to ref count buffer pool pages
141020 16:07:09 [Note] InnoDB: The InnoDB memory heap is disabled
141020 16:07:09 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
141020 16:07:09 [Note] InnoDB: Memory barrier is not used
141020 16:07:09 [Note] InnoDB: Compressed tables use zlib 1.2.7
141020 16:07:09 [Note] InnoDB: Using Linux native AIO
141020 16:07:09 [Note] InnoDB: Using CPU crc32 instructions
141020 16:07:09 [Note] InnoDB: Initializing buffer pool, size = 128.0M
141020 16:07:09 [Note] InnoDB: Completed initialization of buffer pool
2014-10-20 16:07:09 7f6cb59c9880 InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
141020 16:07:09 [ERROR] InnoDB: os_file_get_status() failed on './ibdata1'. Can't determine file permissions
141020 16:07:09 [ERROR] InnoDB: The system tablespace must be writable!
141020 16:07:09 [ERROR] Plugin 'InnoDB' init function returned error.
141020 16:07:09 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
141020 16:07:09 [ERROR] mysqld: File '/mnt/mysql/aria_log_control' not found (Errcode: 13 "Permission denied")
141020 16:07:09 [ERROR] mysqld: Got error 'Can't open file' when trying to use aria control file '/mnt/mysql/aria_log_control'
141020 16:07:09 [ERROR] Plugin 'Aria' init function returned error.
141020 16:07:09 [ERROR] Plugin 'Aria' registration as a STORAGE ENGINE failed.
141020 16:07:09 [Note] Plugin 'FEEDBACK' is disabled.
141020 16:07:09 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
141020 16:07:09 [ERROR] Unknown/unsupported storage engine: InnoDB
141020 16:07:09 [ERROR] Aborting
141020 16:07:09 [Note] /usr/sbin/mysqld: Shutdown complete
141020 16:07:09 mysqld_safe mysqld from pid file /mnt/mysql/femur.pid ended
http://www.reddit.com/r/linuxadmin/comments/2ebhpf/adventures_in_moving_mariadb_data_folder/ helped a bit, but I wasn't able to get it to work.
Any help would be greatly appreciated.

The issue is indeed SELinux; you need to do three things before MariaDB / MySQL will start on CentOS 7:
Ensure the user:group is mysql:mysql
Set the SELinux tag to mysqld_db_t
Set the SELinux user to system_u
This is as simple as:
chcon -Rt mysqld_db_t /database/db
chcon -Ru system_u /database/db
chown -R mysql:mysql /database/db
The whole thing I needed to do after plugging in a disk is below:
cfdisk /dev/sdb
pvcreate /dev/sdb1
vgcreate database /dev/sdb1
lvcreate -l 100%FREE -n db database
mkfs.ext4 /dev/database/db
mkdir /database
mount /database
mkdir /database/db
chcon -Rt mysqld_db_t /database/db
chcon -Ru system_u /database/db
chown -R mysql:mysql /database/db
systemctl start mariadb

Well that was interesting...
It turns out, that CentOS 7 "minimal" installs SELinux, which apparently was preventing mysql from writing to the mounted mirrored set. I was looking for security items that I might not have thought about and found it right there in the docs. I had previously thought (obviously erroneously) that SELinux was a distribution, not a module. Once I ran the test to see if it was there....
getenforce
I temporarily disabled it to test.
setenforce 0
Finally, I was able to start MariaDB with the directory in the mirrored set as the datadir and no errors. To make this change permanent (because this server is behind a firewall), in /etc/selinux/config, I made
- SELINUX=enforcing
+ SELINUX=disabled
I hope this helps someone else. Have a great day!

I found this step by step guide working for me: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/SELinux_Users_and_Administrators_Guide/sect-Managing_Confined_Services-MariaDB-Configuration_Examples.html
You must install:
yum install policycoreutils-python
Guide:
View the SELinux context of the default database location for mysql:
~]# ls -lZ /var/lib/mysql
drwx------. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql
This shows mysqld_db_t which is the default context element for the location of database files. This context will have to be manually applied to the new database location that will be used in this example in order for it to function properly.
Stop the mysqld daemon:
~]# systemctl stop mariadb.service
Create a new directory for the new location of the database(s). In this example, /mysql/ is used:
~]# mkdir -p /mysql
Copy the database files from the old location to the new location:
~]# cp -R /var/lib/mysql/* /mysql/
Change the ownership of this location to allow access by the mysql user and group. This sets the traditional Unix permissions which SELinux will still observe:
~]# chown -R mysql:mysql /mysql
Run the following command to see the initial context of the new directory:
~]# ls -lZ /mysql
drwxr-xr-x. mysql mysql unconfined_u:object_r:usr_t:s0 mysql
The context usr_t of this newly created directory is not currently suitable to SELinux as a location for MariaDB database files. Once the context has been changed, MariaDB will be able to function properly in this area.
Open the main MariaDB configuration file /etc/my.cnf with a text editor and modify the datadir option so that it refers to the new location. In this example the value that should be entered is /mysql:
[mysqld]
datadir=/mysql
Save this file and exit.
Start mysqld. The service should fail to start, and a denial message will be logged to the /var/log/messages file:
~]# systemctl start mariadb.service
Job for mariadb.service failed. See 'systemctl status postgresql.service' and 'journalctl -xn' for details.
However, if the audit daemon is running and with him the setroubleshoot service, the denial will be logged to the /var/log/audit/audit.log file instead:
SELinux is preventing /usr/libexec/mysqld "write" access on /mysql. For complete SELinux messages. run sealert -l b3f01aff-7fa6-4ebe-ad46-abaef6f8ad71
The reason for this denial is that /mysql/ is not labeled correctly for MariaDB data files. SELinux is stopping MariaDB from having access to the content labeled as usr_t. Perform the following steps to resolve this problem:
Run the following command to add a context mapping for /mysql/. Note that the semanageutility is not installed by default. If it missing on your system, install the policycoreutils-pythonpackage.
**~]# semanage fcontext -a -t mysqld_db_t "/mysql(/.*)?"**
This mapping is written to the /etc/selinux/targeted/contexts/files/file_contexts.local file:
~]# grep -i mysql /etc/selinux/targeted/contexts/files/file_contexts.local
/mysql(/.*)? system_u:object_r:mysqld_db_t:s0
Now use the restorecon utility to apply this context mapping to the running system:
**~]# restorecon -R -v /mysql**
Now that the /mysql/ location has been labeled with the correct context for MariaDB, mysqldstarts:
~]# systemctl start mariadb.service
Confirm the context has changed for /mysql/:
~]$ ls -lZ /mysql
drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql
The location has been changed and labeled, and mysqld has started successfully. At this point all running services should be tested to confirm normal operation.

Related

Reinstall mysql-server after upgrade to Ubuntu 18.04 - errors

Mysql needed to be reinstalled after I upgraded to Ubuntu 18.04. I did 'sudo service mysqld stop' before the upgrade, and then the cleanup step must have removed mysql server. When it wasn't found I did:
sudo apt-get remove mysql-server
sudo apt-get install mysql-server
...
Setting up mysql-server-5.7 (5.7.22-0ubuntu18.04.1) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
mysqld: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
Please enable --log-error option or set appropriate redirections for standard output and/or standard error in daemon mode.
Warning: Unable to start the server. Please restart MySQL and run mysql_upgrade to ensure the database is ready for use.
mysqld: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
Please enable --log-error option or set appropriate redirections for standard output and/or standard error in daemon mode.
Warning: Unable to start the server.
...
I then ran 'systemctl status mysql.service' which reported this:
Jul 19 11:21:49 steve-VAIO mysqld[6106]: 2018-07-19T18:21:49.828124Z 0 [ERROR] InnoDB: Operating system error number 2 in a file oper
Jul 19 11:21:49 steve-VAIO mysqld[6106]: 2018-07-19T18:21:49.828132Z 0 [ERROR] InnoDB: The error means the system cannot find the pat
Jul 19 11:21:49 steve-VAIO mysqld[6106]: 2018-07-19T18:21:49.828140Z 0 [ERROR] InnoDB: Could not find a valid tablespace file for `my
Jul 19 11:21:49 steve-VAIO mysqld[6106]: 2018-07-19T18:21:49.828199Z 0 [Warning] InnoDB: Cannot calculate statistics for table `mysql
Jul 19 11:21:49 steve-VAIO mysqld[6106]: 2018-07-19T18:21:49.828232Z 0 [ERROR] Can't open and lock privilege tables: Tablespace is mi
Jul 19 11:21:49 steve-VAIO mysqld[6106]: 2018-07-19T18:21:49.846964Z 0 [Note] InnoDB: Buffer pool(s) load completed at 180719 11:21:4
Jul 19 11:21:49 steve-VAIO mysqld[6106]: 2018-07-19T18:21:49.934673Z 0 [Note] Event Scheduler: Loaded 0 events
Jul 19 11:21:49 steve-VAIO mysqld[6106]: 2018-07-19T18:21:49.934950Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Jul 19 11:21:49 steve-VAIO mysqld[6106]: Version: '5.7.22-0ubuntu18.04.1' socket: '/var/run/mysqld/mysqld.sock' port: 3306 (Ubuntu
Jul 19 11:21:49 steve-VAIO systemd[1]: Started MySQL Community Server.
mysqld is running and my databases are available, but I'm sure this will bite me again when it updates. phpMyAdmin shows the mysql database, which seems healthy. At the top of the list of databases is one called '#mysql50#mysql.bkp'.
Any suggestions for cleaning this up?
After suggestion to run mysql_upgrade, I did:
steve#steve-VAIO:~/workspace/JavascriptCourse$ mysql_upgrade -u root -p
mysql_upgrade: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
Enter password:
Checking if update is needed.
Checking server version.
Running queries to upgrade MySQL server.
mysql_upgrade: [ERROR] 1812: Tablespace is missing for table `mysql`.`plugin`.
So I'll hunt for a fix for this table's tablespace.

Docker can't start MariaDB/MySQL during Docker build

My current Dockerfile looks like this:
FROM ubuntu:14.04
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
ENV LANG en_US.UTF-8
# Maria DB Versions
ENV MARIADB_MAJOR 5.5
ENV MARIADB_VERSION 5.5.55+maria-1~trusty
# Create mysql user and group
RUN groupadd -r mysql && useradd -r -g mysql mysql
# Install needed dependencies
RUN apt-get update && apt-get install -y --no-install-recommends software-properties-common && rm -rf /var/lib/apt/lists/*
# Add the MariaDB PGP key to verify their Debian packages.
RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
# Add MariaDB's repository. We use the Ubuntu 14.04 version as there ain't no MariaDB 5.5 for Ubuntu 16.04.
RUN add-apt-repository 'deb http://mirror.jmu.edu/pub/mariadb/repo/5.5/ubuntu trusty main'
# Install Maria DB and open the access for outside of the container
RUN apt-get update \
&& apt-get install -y --no-install-recommends mariadb-server=$MARIADB_VERSION \
&& rm -rf /var/lib/apt/lists/* \
&& sed -i 's/^\(bind-address\s.*\)/# \1/' /etc/mysql/my.cnf \
&& update-rc.d -f mysql disable
# Run as user mysql
USER mysql
# Start the MariaDB to add a user and create the DB
RUN mysqld
RUN echo "#TODO: Create DB, User and grant access"
# Expose Port
EXPOSE 3306
When building the container using docker build -t testmariadb . I get the following output:
Step 13/17 : RUN mysqld
---> Running in 5aeb49c81f5e
170428 20:00:25 [Note] mysqld (mysqld 5.5.55-MariaDB-1~trusty) starting as process 7 ...
170428 20:00:25 InnoDB: The InnoDB memory heap is disabled
170428 20:00:25 InnoDB: Mutexes and rw_locks use GCC atomic builtins
170428 20:00:25 InnoDB: Compressed tables use zlib 1.2.8
170428 20:00:25 InnoDB: Using Linux native AIO
170428 20:00:25 InnoDB: Initializing buffer pool, size = 256.0M
170428 20:00:25 InnoDB: Completed initialization of buffer pool
InnoDB: The first specified data file ./ibdata1 did not exist:
InnoDB: a new database to be created!
170428 20:00:25 InnoDB: Setting file ./ibdata1 size to 10 MB
InnoDB: Database physically writes the file full: wait...
170428 20:00:25 InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
InnoDB: Database physically writes the file full: wait...
170428 20:00:25 InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
InnoDB: Database physically writes the file full: wait...
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: 127 rollback segment(s) active.
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
170428 20:00:25 InnoDB: Waiting for the background threads to start
170428 20:00:26 Percona XtraDB (http://www.percona.com) 5.5.52-MariaDB-38.3 started; log sequence number 0
170428 20:00:26 [Note] Plugin 'FEEDBACK' is disabled.
170428 20:00:26 [Note] Server socket created on IP: '0.0.0.0'.
170428 20:00:26 [Note] Event Scheduler: Loaded 0 events
170428 20:00:26 [Note] mysqld: ready for connections.
Version: '5.5.55-MariaDB-1~trusty' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
As far as I can tell, it starts the MariaDB, but then is stuck there and doesn't execute a next command. Is there another way to start MariaDB in docker?
1)It is not stuck. The MariaDB start running. But you need to use CMD command and not RUN command to achieve your purpose.
The command CMD, similarly to RUN, can be used for executing a specific command. However, unlike RUN it is not executed during build, but when a container is instantiated using the image being built.
2) The last part of your dockerfile should look like this
#Run as user mysql
USER mysql
RUN echo "#TODO: Create DB, User and grant access"
#Expose Port
EXPOSE 3306
#Right way to run mysqld
CMD ["mysqld"]
For building your image
docker build -t testmariadb .
For running the built image
docker run testmariadb
OR ( for detached mode )
docker run -d testmariadb
3) Please ask your self why are you using ubuntu base image (FROM ubuntu:14.04). According to me you should use https://hub.docker.com/_/mariadb/ if you just want to run mariaDb in the container.
For running container what docker command you are using?
Please follow official Docker file for MariaDB.
Docker MariaDB
Here is latest DockerFile MariaDB
this is wrong
# Start the MariaDB to add a user and create the DB
RUN mysqld
I guess you wanted
CMD mysqld
or such
see from
https://github.com/docker-library/mysql/blob/7a850980c4b0d5fb5553986d280ebfb43230a6bb/8.0/Dockerfile
the end is
EXPOSE 3306
CMD ["mysqld"]
RUN, WORKDIR, ADD, COPY, ENV (and some others) helps you for configuring your docker image, ENTRYPOINT and CMD for starting your software (nginx for a nginx image, Wordpress...)
The doc
https://docs.docker.com/engine/reference/builder/

InnoDB: Operating system error number 13 in a file operation not fixed after changing permissions in /var/lib/mysql/ or disabling selinux

When I run mysql, I get the error of ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2). mysqld gives the error of
150101 12:08:19 [Warning] Can't create test file /var/lib/mysql/lamp-update.lower-test
150101 12:08:19 [Warning] Can't create test file /var/lib/mysql/lamp-update.lower-test
150101 12:08:19 [Warning] One can only use the --user switch if running as root
150101 12:08:19 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
150101 12:08:19 [Note] Plugin 'FEDERATED' is disabled.
mysqld: Can't find file: './mysql/plugin.frm' (errno: 13)
150101 12:08:19 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
150101 12:08:19 InnoDB: The InnoDB memory heap is disabled
150101 12:08:19 InnoDB: Mutexes and rw_locks use GCC atomic builtins
150101 12:08:19 InnoDB: Compressed tables use zlib 1.2.8
150101 12:08:19 InnoDB: Using Linux native AIO
150101 12:08:19 InnoDB: Initializing buffer pool, size = 128.0M
150101 12:08:19 InnoDB: Completed initialization of buffer pool
150101 12:08:19 InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name ./ibdata1
InnoDB: File operation call: 'open'.
InnoDB: Cannot continue operation.
sudo mysqld gives the output of 150101 12:11:59 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
I am running a virtual ubuntu linux server. Mysql has worked without a problem until yesterday, when I got this error.
I have tried to set the permissions of the files in /var/lib/mysql to 660, /var/lib/mysql to 755, and its subdirectories to 700. I then ran sudo chown -R mysql:mysql /var/lib/mysql. Then, I tried rebooting my server, and running mysqld. I got the same error.
Then, I tried disabling selinux by running sudo vim /etc/selinux/semanage.conf and adding SELINUX=disabled to the file as the first line. I rebooted again, and got the same error.
This is the extent of any advice I have seen on stackexchange or other sites.
Does anyone have any ideas for what I could do?
Thank you,
Daniel
Edit:
I "solved" my problem by running mysqld as root, but this doesn't seem like a good idea security wise, so I am still looking for a better solution.
I could solve this by changing the ownership of the folder to mysql from group root (sudo chown -R mysql:root folder). Once I did this, all started to work again.
Regards
In my case SELinux was enabled and by disabling SELinux i was able to start the service
To disable SELlinux, edit
/etc/sysconfig/selinux
and set
SELINUX=disabled
Reboot and verify with command
sestatus
Folder permissions in my case are
chown -R mysql:mysql /mnt/efs/fs1/mysql
chmod -R 750 /mnt/efs/fs1/mysql
where /mnt/efs/fs1/mysql is my data directory
I also troubled with this problem.I used 'setenforce 0' this command and then mysql service successfully start.Maybe you can try it.Good luck
first, type these command below;
chown -R root:mysql /opt/lampp/var/mysql
chown -R root:mysql /opt/lampp/mysql
chmod -R 777 /opt/lampp/var/mysql
chmod -R 777 /opt/lampp/mysql
second, rerun the installer.
it worked for me.

Error establishing a database connection

I have a wordpress site on a VPS running Debian 7 that was working fine when I last visited it and I've made no changes since, but now when I visit the site it says: Error establishing a database connection. It also gives the same error in the admin portal.
I had not changed the password, domain, IP, or anything and checked the wp-config.php and everything in it is accurate.
I logged into my VPS as root and tried to log into mysql to check to see if wordpress was still a user, but it couldn't locate the socket:
root#debian7:~# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
So I tried restarting the mysqld and got:
root#debian7:~# /etc/init.d/mysql restart
[ ok ] Stopping MySQL database server: mysqld.
[FAIL] Starting MySQL database server: mysqld . . . . . . . . . . . . . . failed!
When I try to run mysqld:
root#debian7:~# mysqld
131226 15:36:15 [Warning] Can't create test file /var/lib/mysql/debian7.lower-test
131226 15:36:15 [Warning] Can't create test file /var/lib/mysql/debian7.lower-test
131226 15:36:15 [Note] Plugin 'FEDERATED' is disabled.
131226 15:36:15 InnoDB: The InnoDB memory heap is disabled
131226 15:36:15 InnoDB: Mutexes and rw_locks use GCC atomic builtins
131226 15:36:15 InnoDB: Compressed tables use zlib 1.2.7
131226 15:36:15 InnoDB: Using Linux native AIO
mysqld: Can't create/write to file '/tmp/ibRx51To' (Errcode: 30)
131226 15:36:15 InnoDB: Error: unable to create temporary file; errno: 30
131226 15:36:15 [ERROR] Plugin 'InnoDB' init function returned error.
131226 15:36:15 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
131226 15:36:15 [ERROR] Unknown/unsupported storage engine: InnoDB
131226 15:36:15 [ERROR] Aborting
131226 15:36:15 [Note] mysqld: Shutdown complete
So it seemed to be an access problem, so I checked the /var/lib/mysql directory
root#debian7:~# stat /var/lib/mysql
File: `/var/lib/mysql'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fe00h/65024d Inode: 391599 Links: 6
Access: (0700/drwx------) Uid: ( 104/ mysql) Gid: ( 106/ mysql)
Access: 2013-12-10 15:58:56.401341180 -0500
Modify: 2013-12-10 15:58:56.241341172 -0500
Change: 2013-12-10 15:58:56.241341172 -0500
Birth: -
As you can see, mysql is the owner and has rwx access..
The logs are somehow empty so I can't figure out what happened. I'm not familiar with troubleshooting these kinds of things though. So I could be making novice mistakes.
I'm just trying to get my wordpress site back and it seems the database dsiconnected on its own and the server wont restart. Any ideas?
More:
My /etc/mysql/my.cnf file contains these relevant lines:
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
bind-address = 127.0.0.1
Okay, after troubleshooting a while, it turned out that the entire file system is read-only. I tried making new files and in each case it says it can't cause "Read-only filesystem"
It seems to be a bad image or some sort of change my VPS provider made. So I'll be contacting them.

Mysql often crashes [duplicate]

This question already has answers here:
Amazon EC2, mysql aborting start because InnoDB: mmap (x bytes) failed; errno 12
(7 answers)
Closed 3 years ago.
My Mysql server often crashes, and I need to restart mysql using "service mysqld start" command.
I check the mysql error log file.
130807 22:59:47 mysqld_safe Number of processes running now: 0
130807 22:59:47 mysqld_safe mysqld restarted
130807 22:59:47 [Note] Plugin 'FEDERATED' is disabled.
130807 22:59:47 InnoDB: The InnoDB memory heap is disabled
130807 22:59:47 InnoDB: Mutexes and rw_locks use GCC atomic builtins
130807 22:59:47 InnoDB: Compressed tables use zlib 1.2.5
130807 22:59:47 InnoDB: Using Linux native AIO
130807 22:59:47 InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
130807 22:59:47 InnoDB: Completed initialization of buffer pool
130807 22:59:47 InnoDB: Fatal error: cannot allocate memory for the buffer pool
130807 22:59:47 [ERROR] Plugin 'InnoDB' init function returned error.
130807 22:59:47 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
130807 22:59:47 [ERROR] Unknown/unsupported storage engine: InnoDB
130807 22:59:47 [ERROR] Aborting
130807 22:59:47 [Note] /usr/libexec/mysqld: Shutdown complete
130807 22:59:47 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
I have about 10,000 users and I have a database which have 10,000 tables. Those tables are used for recording the user status. And, when I create a new user table, I use the following code with PDO.
$statusTable = "status_".$uid;
$qstr = "CREATE TABLE IF NOT EXISTS `status`.`$statusTable` (
`prim_id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`message` VARCHAR( 600 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`created_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = INNODB";
$db->query($qstr);
Does this code cause mysql crash? I use EC2 instance with 615MB ram. Thanks!!
Aside from a very bad database design, your question/problem seems to have been posted and answered before.
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
I think the problem is you have many parallel connections to your database which is causing this problem. The problem is not with table, problem is with architecture around it.