Shell scripts to start a MySQL server and client on a non-standard port

By Alvin J. Alexander, devdaily.com

I don't know if these Unix/Linux shell scripts will be of use to any other MySQL users, but I thought I'd share the source code here. In short, the first script shows how to start MySQL on a non-standard port (5150 in this case), and the second script shows how to connect to the server as a MySQL client on a non-standard port (again, 5150).

Here's the shell script that starts the MySQL server on a non-standard port:

#!/bin/sh

#-------------------------#
# program: startserver.sh #
#-------------------------#

nohup ./bin/mysqld \
  --sock=/home/al/mysql/mysql.sock \
  --datadir=/home/al/mysql/mysqldata \
  --basedir=/home/al/mysql/mysql-5.0 \
  --user=al \
  --port=5150 \
  &

And here's the shell script that fires up a MySQL client program and connects to the MySQL server on a non-standard port. Note that I'm connecting to the database as the user root, and I'm connecting to a database named mydatabase:

#!/bin/sh

#-------------------------#
# program: startclient.sh #
#-------------------------#

echo "Connecting to mysql ..."

./bin/mysql  --sock=/home/al/mysql/mysql.sock \
             -u root \
             --port=5150 \
             -p \
             mydatabase

How to run mysqldump when your server is on a non-standard port

Finally, here's another shell script that runs the mysqldump command against the MySQL server that's running on a non-standard port. Again I'm connecting to the database as the user root, and in this case I'm backing up a database named mydatabase:

#!/bin/sh

#---------------------------#
# program: mysqldump5150.sh #
#---------------------------#

echo "Connecting to mysql ..."

DATEFORMAT=`date +"%Y%m%d.%H%M"`
FILENAME="mysqldump.mydatabase.${DATEFORMAT}"

./bin/mysqldump  --sock=/home/al/mysql/mysql.sock \
             -u root \
             --port=5150 \
             -R \
             -p \
             mydatabase \
             > $FILENAME

devdaily logo