使用 mySQL 配置 Vagrant(在 MacBook Pro M2 上)

问题描述 投票:0回答:1

背景

大约 2 年前,我在 MacBook Pro(Intel)上设置了

vagrant/ubuntu
Web 开发环境。效果很好,直到我升级到 M2 MBP 之前,它已经被设置并忘记了。它按照here记录的方式工作,直到安装 PHP。

然后我添加...

config.vm.provision :shell, path: "provision/scripts/mysql.sh", :args => [MYSQL_VERSION, RT_PASSWORD, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_NAME_TEST]

到我的

Vagrantfile
,它正在运行...

#!/bin/bash

# MYSQL_VERSION    $1
# RT_PASSWORD      $2
# DB_USERNAME      $3
# DB_PASSWORD      $4
# DB_NAME          $5
# DB_NAME_TEST     $6

# Install MySQL
apt-get update
apt-get -y upgrade

debconf-set-selections <<< "mysql-server mysql-server/root_password password $2"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $2"

apt-get -y install mysql-server

# Create the database and grant privileges
CMD="sudo mysql -uroot -p$2 -e"

$CMD "CREATE DATABASE IF NOT EXISTS $5"
$CMD "GRANT ALL PRIVILEGES ON $5.* TO '$3'@'%' IDENTIFIED BY '$4';"
$CMD "CREATE DATABASE IF NOT EXISTS $6"
$CMD "GRANT ALL PRIVILEGES ON $6.* TO '$3'@'%' IDENTIFIED BY '$4';"
$CMD "FLUSH PRIVILEGES;"

sed -i "s/.*bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf
grep -q "^sql_mode" /etc/mysql/mysql.conf.d/mysqld.cnf || echo "sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" >> /etc/mysql/mysql.conf.d/mysqld.cnf

service mysql restart

该脚本与我的 GitHub 项目的早期版本和我的生产环境中使用的完全相同。现在失败了...

: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'Pa$$w0rdTw0'' at line 1
: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'Pa$$w0rdTw0'' at line 1
: Job for mysql.service failed because the control process exited with error code.

如果我将

mysql.sh
减少到(有效)...

#!/bin/bash

#RT_PASSWORD     = $2

# Install MySQL
apt-get update

debconf-set-selections <<< "mysql-server mysql-server/root_password password $2"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $2"

apt-get -y install mysql-server

service mysql restart

运行没有错误。鉴于没有错误,我认为 MySQL 上的预期 root 密码已成功设置。

所以我尝试

ssh
进入Vagrant来创建数据库......

vagrant ssh
sudo mysql -uroot -pPa$$w0rd0ne -e "CREATE DATABASE IF NOT EXISTS 'db'"

它失败并出现不同的错误...

mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

sed
中运行
ssh
行不会产生错误,但是运行
grep
行会产生错误...

-bash: /etc/mysql/mysql.conf.d/mysqld.cnf: Permission denied

问题

我必须做什么才能让我的

mysql.sh
脚本再次工作?

mysql ubuntu vagrant apple-silicon
1个回答
0
投票

因为MySQL 8.0中不再支持GRANT语句中隐式创建用户,所以需要先创建用户:

$CMD "CREATE USER '$3'@'%' IDENTIFIED BY '$4';"
$CMD "CREATE DATABASE IF NOT EXISTS $5"
$CMD "GRANT ALL PRIVILEGES ON $5.* TO '$3'@'%'"
$CMD "CREATE DATABASE IF NOT EXISTS $6"
$CMD "GRANT ALL PRIVILEGES ON $6.* TO '$3'@'%'"
© www.soinside.com 2019 - 2024. All rights reserved.