首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在脚本中连接到MySQL服务器会返回错误,但在终端中工作正常

在脚本中连接到MySQL服务器会返回错误,但在终端中工作正常
EN

Stack Overflow用户
提问于 2021-10-12 09:52:03
回答 1查看 83关注 0票数 2

我正在尝试编写一个bash脚本,它将自动安装和设置MySQL服务器。问题是当我的脚本执行以下命令时:

代码语言:javascript
复制
mysql --socket=<path-to-mysql-dir>/mysql/socket -u root -p"$pass"

我得到以下错误:

代码语言:javascript
复制
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/<path-to-mysql-dir>/mysql/socket' (2)

然而,当我在bash终端上运行相同的命令时,它工作得很好:

代码语言:javascript
复制
$ mysql --socket=socket -u root -p'random-init-pass'
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.18

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

这是布景:

我有一个名为sourceme.bash的脚本。我用source sourceme.bash来设置服务器。如下所示( <>中的所有内容都不重要):

mysql/sourceme.bash

代码语言:javascript
复制
source /<path-to-bash-init>/bash
module load <modules>

SQLDIR="/<path-to-mysql-dir>"
cd $SQLDIR

# Kill the existing MySQL server (for testing purposes as I'm re-running this script multiple times)
# Do so by locating its process ID and terminating that ID, waiting until it's killed before proceeding.
ps -aux | grep mysql | grep -v grep
mysqlps=`ps -aux | grep mysql | grep -v grep | sed -e 's/ \{2,\}/ /g' | cut -f2 -d' '`

if [[ -n $mysqlps ]]; then
    kill $mysqlps
    while kill -0 $mysqlps; do
        sleep 0.5
    done
    echo "MySQL process successfully killed. Status $?"
else
    echo "No MySQL process to kill. Status $?"
fi
rm -rf $SQLDIR/mysql

# Set up MySQL server directory
mkdir -p $SQLDIR/mysql
pushd $SQLDIR/mysql

# Copy setup files stored in $SQLDIR in order to install MySQL
# The my.cnf config file contains the string "$USER" which should be replaced with a Unix username.
cat $SQLDIR/setup_files/my.cnf | awk -v user=`whoami` '{ sub(/\$USER/, user); print }' > my.cnf
cp -t . $SQLDIR/setup_files/loadmodules.bash $SQLDIR/setup_files/mysql_*
chmod +x mysql_*

# Load modules necessary to run the server
source loadmodules.bash

# Initialise MySQL server, kill the script if this step fails.
# NB thanks to Raman Sailopal for helping me with this bit: https://stackoverflow.com/questions/69406526/start-a-mysql-server-pipe-the-output-to-a-file
./mysql_init.sh 2>&1 | tee mysql_init.log
init_status=${PIPESTATUS[0]} 
if [[ $init_status -ne 0 ]]; then
    echo "Init script failed - kill script"
    return 1
fi

# Grab the setup password from the log produced by mysql_init.sh. Copy into variable.
line=`tail -n1 mysql_init.log`
pass="${line##* }"

# Launch MySQL server now that it's been initialised
./mysql_start.sh

# Create a .my.cnf config file in the user's home area, set to user-access only
user=`whoami`
cat << EOT > ~/.my.cnf
[mysqld]

user=$user
password=$pass
socket=$SQLDIR/mysql/socket
EOT

chmod 700 ~/.my.cnf

# Fire up the MySQL server command line
# THIS IS THE BIT THAT DOESN'T WORK
mysql --socket=$SQLDIR/mysql/socket -u root -p"$pass"

如果我注释掉最后一行并将完全相同的内容复制到我的终端中,它将启动MySQL服务器,而不会有任何抱怨。我还尝试使用-e选项将命令输入CLI,然后退出,但这也不起作用。

为了完整起见,下面是将位于$SQLDIR/mysql位置的安装文件--脚本运行这个位置是为了启动MySQL服务器。

mysql/my.cnf

代码语言:javascript
复制
[mysqld]
 
user=lou-unix-username
socket=socket

mysql/mysql_init.sh

代码语言:javascript
复制
#! /bin/sh
  
export MYSQL_HOME=$PWD
  
# Change lc-messages-dir if running on RHE6 host

mysqld \
--defaults-file=$MYSQL_HOME/my.cnf \
--lc-messages-dir="/<path-to-mysql>/mysql/5.7.18/rhe7-x86_64/share/english/" \
--datadir=$MYSQL_HOME/data \
--basedir=$MYSQL_HOME \
--pid-file=$MYSQL_HOME/mysql.pid \
--socket=$MYSQL_HOME/socket \
--port=3307 \
--initialize

mysql/mysql_start.sh

代码语言:javascript
复制
#! /bin/sh
echo $DATADIR
export MYSQL_HOME=$PWD
  
mysqld \
--defaults-file=$MYSQL_HOME/my.cnf \
--log-error \
--lc-messages-dir="/<path-to-mysql>/mysql/5.7.18/rhe7-x86_64/share/english/" \
--datadir=$MYSQL_HOME/data \
--basedir=$MYSQL_HOME \
--pid-file=$MYSQL_HOME/mysql.pid \
--socket=$MYSQL_HOME/socket \
--port=3307 &

~/.my.cnf

代码语言:javascript
复制
[mysqld]

user=lou-unix-username
password=random-init-password
socket=/<path-to-mysql-dir>/mysql/socket

有人能提出为什么我可以在终端上启动CLI,但不能在脚本中启动吗?我知道在启动MySQL CLI之后,我需要重置随机初始化的密码,但是它甚至不允许我使用脚本来做这件事。我做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-12 15:28:23

嗯,我不完全确定出了什么问题,但我认为这归结为一个过程在另一个过程开始之前还没有完成。我在脚本中插入了一个sleep 5 --就在我试图连接到服务器的最后一行之前,这起了作用。我还在while循环中包装了行,使它最多可以尝试连接到服务器,如果它失败了,它将再等待5秒。目前,这似乎相当可靠,而且我能够连接到MySQL服务器。

因此,无论是哪种方式,问题似乎都得到了解决。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69538440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档