How to Connect to the MySQL Server

When you need to add or modify information to a database located on the server, you'll need to connect to the MySQL server. In order to do so, you need to know its hostname, port and what username and password you should connect with. These factors can make connecting to the server a little difficult, but there are a variety of ways in which to accomplish it.

Instructions

    • 1

      Connect to a MySQL server running on localhost. The MySQL command-line client's default behavior is to connect to a server running on localhost. This will connect to the server on localhost as the root user with no password. Example:
      mysql -u root

    • 2

      Connect to a MySQL server using a password by adding the -p switch. This will connect to localhost as root and prompt you for a password. Example:
      mysql -u root -p

    • 3

      Connect to a remote host using the -h switch. A remote host is any server other than localhost. Using the -h switch will connect to a MySQL server running on example.com as root and prompting for a password. Example:
      mysql -u root -p -h example.com

    • 4

      Connect to a remote host running on a non-default port, using the -P switch. Note that this command uses a capital P. It should not be confused with the lowercase -p switch, which means prompt for a password. Using the -P switch will connect, as the root user, to a MySQL server running on example.com port 3330 and prompt for a password. Example:
      mysql -u root -p -h example.com -P 3330

    • 5

      Connect to a server and use a database. Specify the database to use on the command-line in place of connecting and manually issuing a USE command. It's faster and makes automated scripts work better. Simply add the name of the database onto the end of the MySQL client command-line. This example connects to the database as root and uses the customers database. Example:
      mysql -u root -p -h example.com -P 3330 customers

Tips & Warnings

  • Sometimes a MySQL server won't be running on the default port (port 3306). The reasons for this can vary. Sometimes a server runs more than one MySQL server, and sometimes it's moved to a "hidden" port to prevent attackers from finding it.

Related Searches:

Comments

You May Also Like

Related Ads

Featured