Connecting to the MySQL Server using MySQL client program

For connecting to the MySQL server you need to use MySQL client program. It needs to be provided parameters such as the hostname, username, password etc. to connect to the MySQL Server; if no parameters are specified the default values are assumed. Any of the connection parameters can be specified to override the default values.

Connecting to a local server specifying username and password

# mysql --user=username --password=mypassword databasename;
# mysql -u username -pmypassword databasename;

There must be no space between the -p option and the following password value.

The following will prompt you for password

# mysql --user=username --password databasename;
# mysql -u username -p databasename;

In case you do not want to specify the database name while connecting you can use:

# mysql -u username -ppassword;

Once you are connected you can select the database using

mysql> use databasename;

Specifying host while connecting to local MySQL server

# mysql --host=localhost --user=username --password databasename;
# mysql -h localhost -u username -p databasename;

Connecting to a Remote MySQL Server

# mysql --host=remote.server.com --user=username --password databasename;
# mysql -h remote.server.com -u username -p databasename;

Specifying a port while connecting the MySQL Server

# mysql --host=remote.server.com --port=13306 --user=username --password databasename;
# mysql -h remote.server.com -P 13306 -u username -p databasename;

Specify a protocol while connecting the MySQL Server

# mysql --host=remote.server.com --port=13306 --protocol=TCP --user=username --password databasename;
# mysql -h remote.server.com -P 13306 --protocol=TCP -u username -p databasename;

If while running the ‘mysql’ command you get an error – “command not found” then it means that either mysql is not installed or is not accessible.

If mysql is installed in your system then your can do one of the following:

  1. Add the ‘mysql’ path in the environment variable (‘PATH’ in Linux)
  2. Change directory to the directory where ‘mysql’ is located and then run the commands
  3. Add the mysql directory path with the command as shown below:
# /usr/bin/mysql -u username -ppassword;

Leave a Comment

Back to top