mysql - 通过 bash 脚本连接到 MySQL 不会执行查询
问题描述
我正在尝试通过 Bash 脚本连接到 localhost 上的 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 |
+--------------------+
在这里保护变量"
推荐阅读
- android - 无法向android studio中保存的号码发送短信
- node.js - 在 Twilio 中调用系统回调时出现 11200 Http 检索错误
- android - 使用改造对 mvvm 中的 Rxjava 进行错误处理
- java - 如何将我的枚举类返回设置为 ArrayList Java
- c# - System.IO.TextWriter.WriteLine(字符串值)引发 OutOfMemory 异常
- wso2 - wso2is 5.9 CustomUserStoremanager 的配置问题
- python - Robotframework:AttributeError:'dict'对象没有属性'has_key'
- mongodb - 在 mongodb 缝合应用程序中创建一个集合
- python - readthedocs构建时如何删除doctest标志?
- c# - 通过 XPath 查找带有方括号内的属性名称的元素