Logging Onto A MySQL Database

28th January 2008

If you have command line access to your MySQL database server you will need to use certain parameters to log in. Most web hosts will not allow you to do this, so you might want to install MySQL into a local computer and give it a go.

To log into mysql you must run the program called mysql with certain parameters. Here is an example.

./mysql -u username

One thing you must realise is that all usernames are associated with a host so if the user you specified can't access the server from this host then you won't get far. To specify the host location enter the -h flag.

./mysql -h hostaddress -u username

If your user is able to access this server then you will be asked for a password. You can set the password in the string using the -p flag. If this doesn't work then leave out the space between the -p and the password.

./mysql -h hostaddress -u username -ppassword

For security reasons the MySQL server might have been told to use a different port than the standard 3306. You can therefore set the port using the -P flag.

./mysql -h hostaddress -u username -ppassword -P 3309

Users are also restricted to the tables and commands that they can use, so you can set a default database to use that database once you have logged in. This is useful to do if you don't have access to administrative commands like SHOW DATABASES. To set the database to be used just add it to the end of the line, no flag is needed.

./mysql -h hostaddress -u username -ppassword -P 3309 test

You should try to log into the MySQL command line if you are trying to learn SQL, but it is useful if you have some special things to do like data importing or carrying out a backup. One alternative to doing it this way is to get hold of the wonderful MySQL GUI tools which will allow you to anything you can do through the command line without having to have an in depth knowledge of SQL syntax.

Add new comment

The content of this field is kept private and will not be shown publicly.