MySQL provides the mysql
command in order to connect remote MySQL servers via the command line interface. The MySQL server may be in a remote system which is located in our LAN or enterprise network or in the interface. We can use the mysql command by specifying the remote MySQL server hostname or IP address.
Enable/Grant Remote Connections In MySQL Database Server
MySQL database server provides some security features to protect against attacks like network-related attacks. Existing users may be restricted to connecting only locally and denied network access even if they provide the correct username and password. This configuration is stored in the database. First, we should connect to the MySQL database locally and enable remote connection via the network with any IP address.
> mysql -u root -p
After connecting the MySQL database server locally use the following GRANT command to provide the remote connection for the user ismail.
mysql> GRANT ALL ON *.* TO ismail@* WITH GRANT OPTION;;
mysql Command Syntax
The mysql command has the following syntax.
mysql -u USER -p -h HOST
- USER is the user we want to connect to the remote MySQL database server.
- HOST is the system that provides the MYSQL database server. The HOST can be specified with a hostname, IP Address, or Domain name.
Connect Remote MySQL Server Using IP Address
We can connect remote MySQL server by using the IP address. We can provide the remote MySQL server IP address by using the -h
option.
> mysql -u ismail -p -h 192.168.1.10
Connect Remote MySQL Server Using Hostname
We can connect remote MySQL server by using the hostname. We can provide the remote MySQL server hostname by using the -h
option.
> mysql -u ismail -p -h db1
Connect Remote MySQL Server Using Domain Name
We can connect remote MySQL server by using the domain name. We can provide the remote MySQL server domain name by using the -h
option.
> mysql -u ismail -p -h database1.winsetut.com