将xscj指定为当前数据库_(2)连接登陆数据库

49ed0517239e420bd7a07ef120c2d598.png
参数说明:

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

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

-S 指定套接字位置

-h 指定远端的mysql主机

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

-e 指定执行的sql语句

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

将密码直接写在文件,方便登陆(不安全)
[root@node2 ~]# more /usr/bin/my
mysql -uroot -p'MoRui!@#'
[root@node2 ~]# 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、交互式输入密码:
[root@node2 ~]# mysql -u root -p
Enter password: 

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

3、在本地连接mysql时,可以指定哪个套接字文件连接到数据库,通过-S指定套接字位置(比较少用)
[root@node2 ~]# 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;'

root@node1:~#  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()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
查看当前时间:
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-03-03 21:36:45 |
+---------------------+
1 row in set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值