MySQL讲义第1讲——MySQL登录和退出
MySQL 登录分为本地登录和远程登录。MySQL 安装完毕后默认生成一个 root 用户,利用该用户登录,添加其他的用户,然后就可以使用新用户登录 MySQL。
一、登录MySQL
登录MySQL的语法如下:
mysql -u用户名 -p用户密码 -h主机名或IP地址 -P端口号
说明:
(1)-u:登录MySQL的用户名;
(2)-p:登录密码;
(3)-h:要登录的 MySQL 服务器的主机名或者IP地址。如果登录本地计算机上的 MySQL,则可以省略此参数;
(4)-P:MySQL 的端口号。如果登录本地计算机上的 MySQL,并且端口号是默认端口,可以省略此参数。
1、登录到本地计算机
[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、添加新用户
添加新用户的命令如下:
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:设置访问密码。
举例:
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 命令刷新权限。
查看用户信息:
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、使用 admin 用户远程登录
如果要登录远程的 MySQL,必须在要登录的服务器的 MySQL 中添加相应的登录用户并设置权限。比如 IP 地址为 192.168.1.11 的服务器中的 MySQL 用户信息如下:
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)
使用 admin 用户登录远程登录 MySQL:
[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>
二、退出MySQL
退出 MySQL 使用 quit 或者 exit 命令。
mysql> exit
Bye
[root@localhost ~]#