MySQL: How to start a MySQL server and client on a non-standard port

MySQL shell scripts and port FAQ: Can you share some MySQL examples that show how to start MySQL on a non-standard port (non default port)?

For a variety of reasons you may want or need to run your MySQL server on a different port than the default MySQL port of 3306. In those cases the easiest thing you can do is create a Unix/Linux shell script to start your MySQL server on some other port.

In this tutorial I share a couple of MySQL shell scripts that show how this can be done. The first shell script shows how to start MySQL on a non-standard port (5150 in this case), and the second MySQL script shows how to connect to the server as a MySQL client on a non-standard port (again, 5150).

How to start the MySQL server on a different port

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 \
  &

How to start the MySQL client on a non-standard port

And here’s the shell script that fires up a MySQL command-line 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