(2)连接登陆数据库

时间:2021-11-18 来源:未知网络 作者:996建站网
参数说明:

-u 表示指定用户,-u与root用户名之间的空格可省略

-p 表示会提示输入密码,-p与密码之间不能存在空格

-S 指定套接字位置

-h 指定远端的mysql主机

-P 大写,指定musql服务对应的端口

-e 指定执行的sql语句

-------------------------------
1、本地连接
2、远程连接(多用于脚本)

将密码直接写在文件,方便登陆(不安全)
[[email protected] ~]# more /usr/bin/my
mysql -uroot -p'MoRui!@#'
[[email protected] ~]# my
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 3
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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>


1、交互式输入密码:
[[email protected] ~]# mysql -u root -p
Enter password: 

2、指定密码:
[[email protected] ~]# mysql -u root -p123456

3、在本地连接mysql时,可以指定哪个套接字文件连接到数据库,通过-S指定套接字位置(比较少用)
[[email protected] ~]# mysql -uroot -p123456 -S  /var/lib/mysql/mysql.sock

4、连接到远程mysql主机
-h 指定远端的mysql主机
-P 大写,指定musql服务对应的端口
连接的前提是已经授权当前客户端的ip地址能够连接到数据库
mysql -u root -p123456 -h 192.168.100.50 -P 3306

5、连接数据库的同时执行对应的命令(常用于shell脚本中)
-e 指定执行的sql语句
mysql -u root -p123456 -e 'use mysql;select user,host,password form user;'

[email protected]:~#  mysql -uroot -p'Study2019!@#' -h 119.27.187.118 -P 3306  -e 'use mysql;select user,host,authentication_string from user;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+-----------+-------------------------------------------+
| user      | host      | authentication_string                     |
+-----------+-----------+-------------------------------------------+
| root      | localhost | *6F77D0F641432E14D4AAE5CCAF1E4341A241EAAC |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+-----------+-------------------------------------------+

mysql -u root -p123456 -e 'create database if not exists testdb; show databases;'
查看数据库版本:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.25    |
+-----------+
1 row in set (0.00 sec)
查看当前用户:
mysql> select user();
+----------------+
| user()         |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)
查看当前时间:
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-03-03 21:36:45 |
+---------------------+
1 row in set (0.00 sec)

(2)连接登陆数据库插图

微信扫一扫 关注公众号

微信扫一扫 使用小程序

百度扫一扫 使用小程序