MySQL Lecture 1-MySQL Login and Logout

MySQL Lecture 1-MySQL Login and Logout

MySQL login is divided into local login and remote login. After MySQL is installed, a root user is generated by default. Use this user to log in, add other users, and then use the new user to log in to MySQL.

One, log in to MySQL

The syntax for logging in to MySQL is as follows:

mysql -u用户名 -p用户密码 -h主机名或IP地址 -P端口号
 
说明:
(1-u:登录MySQL的用户名;
(2-p:登录密码;
(3-h:要登录的 MySQL 服务器的主机名或者IP地址。如果登录本地计算机上的 MySQL,则可以省略此参数;
(4-P:MySQL 的端口号。如果登录本地计算机上的 MySQL,并且端口号是默认端口,可以省略此参数。

1. Log in to the local computer

[root@localhost ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

2. Add new users

The command to add a new user is as follows:

grant 权限 on 数据库.to '用户名'@'允许访问的IP地址' identified by '密码';

说明:
(1)权限:指定登录用户的访问权限。比如 SELECT、CREATE、DELETE、DELETE、ALTER 等,可以使用 ALL 授予用户所有权限;
(2)数据库.表:可以限制用户对哪些表执行 SELECT、CREATE、DELETE、DELETE、ALTER 等操作。
例如:stu.dept 表示能够访问 stu 数据库中的dept表;
     stu.* 表示能够访问 stu 数据库中的所有t表;
     *.* 表示能够访问所有数据库中的所有表;
(3)用户名@允许访问的IP地址:只能用户以及用户可以通过哪些 IP 地址来访问,可以使用通配符 %。
例如:'wgx'@'192.168.0.12' 表示用户 wgx 只能通过IP地址为 '192.168.0.12' 的计算机访问;
     'wgx'@'192.168.0.%' 表示用户 wgx 可以通过 '192.168.0.%' 网段的计算机访问;
     'wgx'@'%' 表示用户 wgx 可以通过任何的计算机访问。
(4)identified by:设置访问密码。

For example:

mysql> grant all on *.* to 'admin'@'192.168.0.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.06 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

--说明:添加用户之后,需要使用 flush privileges 命令刷新权限。

View user information:

mysql> select user,host from mysql.user;
+---------------+-------------+
| user          | host        |
+---------------+-------------+
| admin         | 192.168.0.% |
| repl          | 192.168.0.% |
| wgx           | 192.168.0.% |
| mysql.session | localhost   |
| mysql.sys     | localhost   |
| root          | localhost   |
+---------------+-------------+
6 rows in set (0.00 sec)

3. Use the admin user to log in remotely

If you want to log in to remote MySQL, you must add the corresponding login user and set permissions in the MySQL of the server to log in. For example, the MySQL user information in the server with IP address 192.168.1.11 is as follows:

mysql> select user,host from user;
+---------------+-------------+
| user          | host        |
+---------------+-------------+
| admin         | 192.168.0.% |
| repl          | 192.168.0.% |
| wgx           | 192.168.0.% |
| mysql.session | localhost   |
| mysql.sys     | localhost   |
| root          | localhost   |
+---------------+-------------+
6 rows in set (0.00 sec)

Log in to MySQL remotely as the admin user:

[root@localhost ~]# mysql -uadmin -p123456 -h192.168.0.11 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.23-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

Two, exit MySQL

To exit MySQL, use the quit or exit command.

mysql> exit
Bye
[root@localhost ~]# 

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/108644225