gpt4 book ai didi

mysql - 通过bash脚本连接MySQL不执行查询

转载 作者:行者123 更新时间:2023-11-29 15:31:40 24 4
gpt4 key购买 nike

我正在尝试通过 Bash 脚本连接到本地主机上的 mysql。(在 Raspberry PI 3B+ Raspbian 上)

连接似乎可以工作,但它不显示查询结果,而是显示某种 mysql 帮助页面。看起来像这样:

mysql  Ver 15.1 Distrib 10.1.38-MariaDB, for ... using readline 5.2
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Usage: mysql [OPTIONS] [database]

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysql client client-server client-mariadb
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
...
#!/bin/bash
username="user"
passwort="1234"
mysql -u $username -p$passwort -e "show databases;"

如何更改命令来执行给定的语句?

最佳答案

您的密码包含一些由 shell 解释的字符,您必须通过引用来保护密码。

示例我的密码有一个 (空格)

MyMac:~ e444$ u="root"
MyMac:~ e444$ p="1234 2"
MyMac:~ e444$ mysql -u$u -p$p -h 127.0.0.1 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'172.17.0.1' (using password: YES)

此处收到访问被拒绝的消息,因为发送的密码是1234

MyMac:~ e444$ mysql -u$u -p"$p" -h 127.0.0.1 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+

这里用"保护变量

关于mysql - 通过bash脚本连接MySQL不执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58681573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com