Accessing MySQL via SSH or Shell

If you are using any web host or server they may provide shell access most of the shared web hosting providers will not give shell access due to security reasons.

If you own a  server or shell access and like to work MySQL commands in shell. Let’s see how to connect MySQL via SSH or Shell.

Command to connect MySQL via Shell

mysql -u(username) -h (hostname) -p(password)



user@tothetech.net [~]# mysql -u username -h localhost -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 35642
Server version: 5.0.67-community MySQL Community Edition (GPL)

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql>

If you are using root log in in SSH or Shell access then no need to mention about password for connecting MySQL in SSH



root@server [~]# mysql -u root -h localhost
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 35874
Server version: 5.0.67-community MySQL Community Edition (GPL)

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql>