Skip to content

Instantly share code, notes, and snippets.

@neomatrixcode
Forked from nihilismus/mysql.md
Last active August 29, 2015 14:19
Show Gist options
  • Save neomatrixcode/dfd14c355884bb605a7c to your computer and use it in GitHub Desktop.
Save neomatrixcode/dfd14c355884bb605a7c to your computer and use it in GitHub Desktop.

/etc/rc.d/rc.mysqld-instancia1

#!/bin/sh

# Depende del usuario bajo el cual se ha de ejecutar el demonio
USER_DB=mysql

# Los directorios y archivos deben permitir a $USER_DB el acceso a lectura y escritura
DATADIR_DB=/home/mysql/instancia1
LOG_ERROR_DB=/var/log/mysql/instancia1.err
PID_FILE_DB=/var/run/mysql/instancia1.pid
SOCKET_DB=/var/run/mysql/instancia1.sock
CONFIG_FILE=$DATADIR_DB/instancia1.cnf

# Puerto para conexiones de red
PORT_DB=5000
# Desactiva la espera de conexiones de red
# SKIP="--skip-networking"

# No suelen modificarse
BASEDIR_DB=/usr
PLUGINDIR_DB=/usr/lib/mysql/plugin

# Start mysqld:
mysqld_start() {
  if [ -x /usr/bin/mysqld_safe ]; then
    # If there is an old PID file (no mysqld running), clean it up:
    if [ -r $PID_FILE_DB ]; then
      if ! ps axc | grep mysqld 1> /dev/null 2> /dev/null ; then
        echo "Cleaning up old $PID_FILE_DB"
        rm -f $PID_FILE_DB
      fi
    fi
    /usr/bin/mysqld_safe \
        --defaults-file=$CONFIG_FILE \
        --basedir=$BASEDIR_DB \
        --datadir=$DATADIR_DB \
        --plugin-dir=$PLUGINDIR_DB \
        --user=$USER_DB \
        --log-error=$LOG_ERROR_DB \
        --pid-file=$PID_FILE_DB \
        --socket=$SOCKET_DB \
        --port=$PORT_DB \
  $SKIP &
  fi
}

# Stop mysqld:
mysqld_stop() {
  # If there is no PID file, ignore this request...
  if [ -r $PID_FILE_DB ]; then
    kill $(cat $PID_FILE_DB)
    # Wait at least one minute for it to exit, as we don't know how big the DB is...
    for second in 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 \
      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 60 ; do
      if [ ! -r $PID_FILE_DB ]; then
        break;
      fi
      sleep 1
    done
    if [ "$second" = "60" ]; then
      echo "WARNING:  Gave up waiting for mysqld to exit!"
      sleep 15
    fi
  fi
}

# Restart mysqld:
mysqld_restart() {
  mysqld_stop
  mysqld_start
}

case "$1" in
'start')
  mysqld_start
  ;;
'stop')
  mysqld_stop
  ;;
'restart')
  mysqld_restart
  ;;
*)
  echo "usage $0 start|stop|restart"
esac

/etc/rc.d/rc.postgresql-instancia1

#!/bin/bash

LOGFILE=/var/log/postgresql-instancia1.log
DATADIR=/home/postgres/instancia1
POSTGRES=/usr/bin/postgres
PIDFILE=postmaster.pid
SOCKETDIR=/var/run/postgresql
PORT=6000

# oom-killer score
# if defined and set to -1000, main postmaster wont be killed
# by Linux OOM killer, but individual backends still could be
# (since OOM_SCORE_ADJ in SlackBuild is set to 0)
#
# http://www.kernel.org/doc/Documentation/filesystems/proc.txt
OOM_SCORE_ADJ=-1000

# Return values (according to LSB):
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running

pg_ctl()
{
  CMD="/usr/bin/pg_ctl $@"
  su - postgres -c "$CMD"
}

if [ ! -f $POSTGRES ]; then
	echo "Could not find 'postgres' binary. Maybe PostgreSQL is not installed properly?"
	exit 5
fi

case "$1" in

	"start")
		echo "Starting PostgreSQL"
		touch $LOGFILE
		chown postgres:wheel $LOGFILE
		chmod 0640 $LOGFILE
	
		if [ ! -e $DATADIR/PG_VERSION ]; then
			echo "You should initialize the PostgreSQL database at location $DATADIR"
			exit 6
		fi
	
		if [ $(pgrep -f "$POSTGRES -D $DATADIR" ) ]; then

			echo "PostgreSQL daemon already running"
			if [ ! -f $DATADIR/$PIDFILE ]; then
				echo "Warning: Missing pid file $DATADIR/$PIDFILE"
			fi
			exit 1

		else # remove old socket, if it exists and no daemon is running.

			if [ ! -f $DATADIR/$PIDFILE ]; then
				rm -f /tmp/.s.PGSQL.5432
				rm -f /tmp/.s.PGSQL.5432.lock
				test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj
				pg_ctl start -w -l $LOGFILE -D $DATADIR -o "'-p $PORT -k $SOCKETDIR'"
				exit 0
			else
				echo "PostgreSQL daemon was not properly shut down"
				echo "Please remove stale pid file $DATADIR/$PIDFILE"
				exit 7
			fi

		fi	
	;;

	"stop")
		echo "Shutting down PostgreSQL..."
		pg_ctl stop -l $LOGFILE -D $DATADIR -m smart
	;;

	"force-stop")
		# Take care! This will kill _all_ client connections
		# and rollback current transactions.
		echo "Shutting down PostgreSQL (fast)..."
		pg_ctl stop -l $LOGFILE -D $DATADIR -m fast
	;;
	
	"unclean-stop")
		# Take care! This will abort server process itself
		# resulting with database recovery on next start.
		echo "Shutting down PostgreSQL (immediate)..."
		pg_ctl stop -l $LOGFILE -D $DATADIR -m immediate
	;;

	"restart")
		echo "Restarting PostgreSQL..."
		test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj
		pg_ctl restart -l $LOGFILE -D $DATADIR -m smart
	;;

	"force-restart")
		# Take care! This will kill _all_ client connections
		# and rollback current transactions.
		echo "Restarting PostgreSQL (fast)..."
		pg_ctl restart -l $LOGFILE -D $DATADIR -m fast
	;;

	"unclean-restart")
		# Take care: This will abort server process itself
		# resulting with database recovery on start.
		echo "Restarting PostgreSQL (immediate)..."
		pg_ctl restart -l $LOGFILE -D $DATADIR -m immediate
	;;

	"reload")
		echo "Reloading configuration for PostgreSQL..."
		pg_ctl reload -l $LOGFILE -D $DATADIR -m smart
	;;

	"status")
		if [ $(pgrep -f $POSTGRES) ]; then
			echo "PostgreSQL is running"

			if [ ! -e $DATADIR/$PIDFILE ]; then
				echo "Warning: Missing pid file $DATADIR/$PIDFILE"
			fi

			exit 0
		else
			echo "PostgreSQL is stopped"

			if [ -e $DATADIR/$PIDFILE ]; then
				echo "Detected stale pid file $DATADIR/$PIDFILE"
			fi

			exit 0
		fi
	;;

	*)
		# unclean-stop and unclean-restart are not documented on purpose.
		echo "Usage: $0 {start|stop|force-stop|status|restart|force-restart|reload}"
		exit 1
	;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment