Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t从外部bash脚本正确设置MySQL密码_Mysql_Linux_Bash_Ubuntu_Debconf - Fatal编程技术网

Can';t从外部bash脚本正确设置MySQL密码

Can';t从外部bash脚本正确设置MySQL密码,mysql,linux,bash,ubuntu,debconf,Mysql,Linux,Bash,Ubuntu,Debconf,我有两个脚本——主脚本执行一些不同的操作并调用第二个脚本,第二个脚本安装MySQL 根据我的主要剧本,我做了如下工作: ... read -p "Set the password for the database [min. 4 characters]: " MPASS # Install MySQL path/to/setup-mysql.sh "$MPASS" mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO

我有两个脚本——主脚本执行一些不同的操作并调用第二个脚本,第二个脚本安装MySQL

根据我的主要剧本,我做了如下工作:

...

read -p "Set the password for the database [min. 4 characters]: " MPASS

# Install MySQL
path/to/setup-mysql.sh "$MPASS"

mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '${MPASS}';"
service mysql restart

mysql --user="root" --password=${MPASS} -e "SET GLOBAL validate_password_policy = 'LOW'; SET GLOBAL validate_password_length = 4; CREATE USER 'johndoe'@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO 'johndoe'@'${IPADDRESS}' IDENTIFIED BY '${MPASS}' WITH GRANT OPTION;"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO 'johndoe'@'%' IDENTIFIED BY '${MPASS}' WITH GRANT OPTION;"
mysql --user="root" --password=${MPASS} -e "FLUSH PRIVILEGES;"
#!/bin/bash

export DEBIAN_FRONTEND=noninteractive

debconf-set-selections <<< "mysql-community-server mysql-community-server/data-dir select ''"
debconf-set-selections <<< "mysql-community-server mysql-community-server/root-pass password ${1}"
debconf-set-selections <<< "mysql-community-server mysql-community-server/re-root-pass password ${1}"

apt-get install -y mysql-server

# Start mysql on boot
update-rc.d mysql defaults

# Configure Password Expiration
echo "default_password_lifetime = 0" >> /etc/mysql/my.cnf

# Configure Access Permissions For Root
sed -i '/^bind-address/s/bind-address.*=.*/bind-address = */' /etc/mysql/my.cnf
setup mysql.sh
如下所示:

...

read -p "Set the password for the database [min. 4 characters]: " MPASS

# Install MySQL
path/to/setup-mysql.sh "$MPASS"

mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '${MPASS}';"
service mysql restart

mysql --user="root" --password=${MPASS} -e "SET GLOBAL validate_password_policy = 'LOW'; SET GLOBAL validate_password_length = 4; CREATE USER 'johndoe'@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO 'johndoe'@'${IPADDRESS}' IDENTIFIED BY '${MPASS}' WITH GRANT OPTION;"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO 'johndoe'@'%' IDENTIFIED BY '${MPASS}' WITH GRANT OPTION;"
mysql --user="root" --password=${MPASS} -e "FLUSH PRIVILEGES;"
#!/bin/bash

export DEBIAN_FRONTEND=noninteractive

debconf-set-selections <<< "mysql-community-server mysql-community-server/data-dir select ''"
debconf-set-selections <<< "mysql-community-server mysql-community-server/root-pass password ${1}"
debconf-set-selections <<< "mysql-community-server mysql-community-server/re-root-pass password ${1}"

apt-get install -y mysql-server

# Start mysql on boot
update-rc.d mysql defaults

# Configure Password Expiration
echo "default_password_lifetime = 0" >> /etc/mysql/my.cnf

# Configure Access Permissions For Root
sed -i '/^bind-address/s/bind-address.*=.*/bind-address = */' /etc/mysql/my.cnf
上面说密码是错的。当我尝试手动登录时,它不会工作。当我跳过密码字段(
mysql-u root
)时,它就工作了。所以根本没有设置密码

另外,当我在
setup mysql.sh
中执行
echo$1
时,它正确地显示了
mpas
包含的内容


为什么不为MySQL安装正确设置密码。我在这里真的很困惑。

看起来您正在编写一个MySQL安装后初始化shell脚本

在我个人看来:改变你的shell脚本设计方法。

在脚本中,使用
mysql--user=“root”--password=${MPASS}
提示错误信息。您可以使用mysql参数--defaults文件,将mysql登录用户
root
和相应的密码写入临时文件,如

[client]
user=root
password=YOURPASSWORD
假设文件路径是
/tmp/mysql.cnf
(分配变量
mysql\u my\u cnf

然后使用
mysql--defaults file=“$mysql\u my\u cnf”
login,它将成功登录

将变量分配给
mysql--defaults file=“$mysql\u my\u cnf”
,例如
mysql\u命令

旧方法 新方法 你可以试试

附言 我写了一封信,可能对你有帮助

${mysql_command} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"