MySQL 8 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Connecting to the MySQL client can be done with any of the following commands:

shell> mysql -h localhost -P 3306 -u <username> -p<password>
shell> mysql --host=localhost --port=3306 --user=root --password=<password>
shell> mysql --host localhost --port 3306 --user root --password=<password>

It is highly recommended not to give the password in the command line, instead you can leave the field blank; you will be prompted for a password:

shell> mysql --host=localhost --port=3306 --user=root --password  
Enter Password:
  1. The  -P argument (in uppercase) is passed for specifying the port.
  2. The  -p argument (in lowercase) is passed for specifying the password.
  3. There is no space after the  -p argument.
  4. For the password, there is no space after =.

By default, the host is taken as localhost, the port is taken as 3306, and the user is taken as the current shell user.

  1. To know the current user:
shell> whoami
  1. To disconnect, press Ctrl D or type exit:
mysql> ^DBye
shell>

Or use:

mysql> exit;
Bye
shell>
  1. After connecting to the mysql prompt, you can execute statements followed by the delimiter. The default delimiter is a semicolon (;):
mysql> SELECT 1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
  1. To cancel a command, press Ctrl C or type \c:
mysql> SELECT ^C
mysql> SELECT \c
Connecting to MySQL using the root user is not recommended. You can create users and restrict the user by granting appropriate privileges, which will be discussed in the  Creating Users  and  Granting and revoking access to users sections . Till then, you can use the root user to connect to MySQL.