Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts
,

Use a script to restart critical Linux services such as the web or database server. Restart nginx, apache2, MySQL or PHP-fpm automatically.

 If you manage your own WordPress web server then you have undoubtedly come across many situations where your web apache, nginx, MySQL or PHP-fpm services have stopped.

Sometimes the reason is unknown and things just crash from time to time.

Using the Linux crontab service we can write a simple bash script to test to see if these services have stopped and restart them.

You can use whatever editor you are comfortable with.

Here’s the command line code to create the file in nano:

sudo nano /opt/launch-crashed-services.sh

Here is the bash script.

#!/bin/bash

service mysql status | grep 'active (running)' > /dev/null 2>&1

if [ $? != 0 ]
then
        sudo service mysql restart > /dev/null
fi

service nginx status | grep 'active (running)' > /dev/null 2>&1

if [ $? != 0 ]
then
        sudo service nginx restart > /dev/null
fi

service php7.2-fpm status | grep 'active (running)' > /dev/null 2>&1

if [ $? != 0 ]
then
        sudo service php7.2-fpm restart > /dev/null
fi

Change the service names to the ones you are running, e.g. “apache2” or whatever PHP version you are running.

The script uses the service <name> status command to output the status of a particular service such as mysql.

We then run this through grep looking for the phrase “active (running)”.

If this is not found, we ask the system to restart the service.

Save the file to /opt/launch-crashed-services.sh

Then ensure that it is runnable from the command line using:

sudo chmod +x /opt/launch-crashed-services.sh

Scheduling Service Restarts Using Crontab

It would be a pain to have to SSH into our server every time a service crashes to run the script.

Instead we can call the script directly from a crontab service and have it running as frequently as we need it to.

Edit your root crontab list using:

sudo crontab -e

It’s important to use the root crontab using the command above and not to edit your own user profile crontab, otherwise, it will not work properly.

Add the following line to the bottom of the root crontab list:

*/1 * * * * /opt/launch-crashed-services.sh > /dev/null 2>

This will run the script every minute but you can change that for whatever works for your server.

Now if a critical service crashes, the server will attempt to restart it.

Happy days.

Continue reading Use a script to restart critical Linux services such as the web or database server. Restart nginx, apache2, MySQL or PHP-fpm automatically.
, ,

How to Install MySQL 5.6 Server on CentOS / Redhat 6/7/8

 Hello Friends, In this article I am going to explain how we can install MySQL 5.6 server in CentOS and Redhat 6/7/8 using yum command.

System requirements :

  • Server : CentOS release 6.7 (Final)
  • Architecture : i686 i386 GNU/Linux

Follow the below steps to install MySQL 5.6 server using yum command.

Install MySQL 5.6 server on CentOS and Redhat 

Login into server using ssh protocol. Download and install mysql-community repo using below command.

# ssh root@serve_ip_and _hostname
# mkdir /root/dowload/
# cd /root/download/

Install wget

first of all install wget command on your machine if it is not installed by default. follow this below command to install wget.

# yum install wget

You will get back some output like below if you have already installed wget package.

Loaded plugins: fastestmirror
Setting up Install Process
\Loading mirror speeds from cached hostfile
* base: mirror.nbrc.ac.in
* epel: ftp.riken.jp
* extras: mirror.nbrc.ac.in
* updates: mirror.nbrc.ac.in
Package wget-1.12-8.el6.i686 already installed and latest version
Nothing to do

Download and Install mysql-community repo

Now download and install mysql-community repo

# wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

--2016-11-25 06:30:03-- http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
Resolving repo.mysql.com... 104.108.196.141
Connecting to repo.mysql.com|104.108.196.141|:80... connected.

Install mysql 5.6 server using yum command

Let’s install MysQL 5.6 server following below command:

#yum install mysql-server

Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirror.nbrc.ac.in
* epel: epel.mirror.net.in
* extras: mirror.nbrc.ac.in
* remi: mirrors.thzhost.com
* remi-php56: mirrors.thzhost.com
* remi-safe: mirrors.thzhost.com
* updates: mirror.nbrc.ac.in
ajenti | 2.5 kB 00:00
vz-base | 951 B 00:00
vz-updates | 951 B 00:00
Package mysql-server is obsoleted by mysql-community-server, trying to install mysql-community-server-5.6.34-2.el6.i686 instead
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.i686 0:5.6.34-2.el6 will be installed
................................
....................
Install 5 Package(s)
Total size: 73 M
Installed size: 299 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : mysql-community-common-5.6.34-2.el6.i686 1/5
Installing : mysql-community-libs-5.6.34-2.el6.i686 2/5
Installing : mysql-community-client-5.6.34-2.el6.i686 3/5
Installing : numactl-2.0.9-2.el6.i686 4/5
Installing : mysql-community-server-5.6.34-2.el6.i686 5/5
Verifying : mysql-community-common-5.6.34-2.el6.i686 1/5
Verifying : mysql-community-client-5.6.34-2.el6.i686 2/5
Verifying : numactl-2.0.9-2.el6.i686 3/5
Verifying : mysql-community-libs-5.6.34-2.el6.i686 4/5
Verifying : mysql-community-server-5.6.34-2.el6.i686 5/5
Installed:
mysql-community-server.i686 0:5.6.34-2.el6
Dependency Installed:
mysql-community-client.i686 0:5.6.34-2.el6 mysql-community-common.i686 0:5.6.34-2.el6 mysql-community-libs.i686 0:5.6.34-2.el6
numactl.i686 0:2.0.9-2.el6
Complete!

After installation completed restart mysql service using below command.

#service mysqld start

Or

#/etc/init.d/ mysqld start

Note : We have installed MySQL server with blank root password. We will need to reset root password using “mysql_secure_installation” follow below instructions to reset password.

mysql_secure_installation

Follow this below command to install mysql secure installation.

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): <ENTER>
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] <Type here “Y”>
New password:*****
Re-enter new password:*****
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <Type here “Y”>
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] < Type “ Y “ if you want to disable root remote loging for security purpose>
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] < Type “Y” >
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
... skipping.
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...

Now login with you root password

#mysql -uroot -p

Enter password:*****

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 14

Server version: 5.6.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

I hope this article will be helpful you to install MySQL 5.6 server on Centos and Redhat. If you have any queries and problem please comment in comment section.

Continue reading How to Install MySQL 5.6 Server on CentOS / Redhat 6/7/8
, , ,

How to start, stop & restart MariaDB server in Linux

 MariaDB is a MySQL database management system and popular nowadays.

It is one of the pillars of the LAMP stack popular among developers. In this article, we will walk you through steps to manage the MariaDB server process from the command line shell prompt.

In newer systems like RHEL 7, Debian 8, Ubuntu 15.04, Suse 12 and later system V init daemon is replaced by systemd. We will see both systemd and system V init commands to manage the MariaDB server process. Be noted that we are dealing with the MariaDB server process only. You have to take care of the databases you build within for their graceful shutdown and startup.

How to start MariaDB server

On newer systems with systemd

# systemctl start mariadb

On older systems with system V init

# service mysql start
Starting MariaDB.190110 07:15:32 mysqld_safe Logging to '/var/lib/mysql/kerneltalks.test.err'.
190110 07:15:32 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
.                                                          [  OK  ]

How to start MariaDB server at boot

On newer systems with systemd

# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'

On older systems with system V init

Systems like RHEL, CentOS, SUSE uses chkconfig to manage processes at boot.

# chkconfig --add mysql
# chkconfig --level 345 mysql on

Systems like Debian, ubuntu uses update-rc.d

# update-rc.d mysql defaults

How to stop MariaDB server

On newer systems with systemd

# systemctl stop mariadb

On older systems with system V init

# service mysql stop
Shutting down MariaDB.                                     [  OK  ]

How to check MariaDB server status

On newer systems with systemd

# systemctl status mariadb
mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled)
   Active: active (running) since Thu 2019-01-10 02:32:04 EST; 1min 35s ago
 Main PID: 10853 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─10853 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─11015 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-fi...
 
Jan 10 02:32:00 ip-172-31-89-93.ec2.internal mariadb-prepare-db-dir[10775]: MySQL manual for more instructions.
Jan 10 02:32:00 ip-172-31-89-93.ec2.internal mariadb-prepare-db-dir[10775]: Please report any problems at http://mariadb.org/jira
Jan 10 02:32:00 ip-172-31-89-93.ec2.internal mariadb-prepare-db-dir[10775]: The latest information about MariaDB is available at http://mariadb.org/.
Jan 10 02:32:00 ip-172-31-89-93.ec2.internal mariadb-prepare-db-dir[10775]: You can find additional information about the MySQL part at:
Jan 10 02:32:00 ip-172-31-89-93.ec2.internal mariadb-prepare-db-dir[10775]: http://dev.mysql.com
Jan 10 02:32:00 ip-172-31-89-93.ec2.internal mariadb-prepare-db-dir[10775]: Consider joining MariaDB's strong and vibrant community:
Jan 10 02:32:00 ip-172-31-89-93.ec2.internal mariadb-prepare-db-dir[10775]: https://mariadb.org/get-involved/
Jan 10 02:32:01 ip-172-31-89-93.ec2.internal mysqld_safe[10853]: 190110 02:32:01 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Jan 10 02:32:01 ip-172-31-89-93.ec2.internal mysqld_safe[10853]: 190110 02:32:01 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Jan 10 02:32:04 ip-172-31-89-93.ec2.internal systemd[1]: Started MariaDB database server.

On older systems with system V init

# service mysql status
MariaDB running (2122)                                     [  OK  ]

How to restart MariaDB server process

On newer systems with systemd

root@kerneltalks # systemctl restart mariadb

On older systems with system V init

root@kerneltalks # service mysql restart
Shutting down MariaDB....                                  [  OK  ]
Starting MariaDB.190110 07:35:01 mysqld_safe Logging to '/var/lib/mysql/ip-172-31-90-145.ec2.internal.err'.
190110 07:35:01 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
.                                                          [  OK  ]

Continue reading How to start, stop & restart MariaDB server in Linux