shell - 从 localhost 授予对 root 的访问权限

标签 shell ubuntu vagrant

我正在关注 link关于如何使用 Vagrant 设置 LAMP 堆栈。

因为该链接使用 wordpress,所以我不得不稍微修改一下代码。但是我收到一个错误:
access denied for user 'root'@'localhost' (using password: YES)
当我尝试在我的 shell 文件中创建用户、创建数据库等时会发生此错误。我已在线搜索此错误,它表明我需要从 localhost 授予对 root 的访问权限。我不确定如何在我的 provisions.sh 中执行此操作.任何人都可以提供一些建议吗?

这是我的 shell 文件:

#!/usr/bin/env bash

#set variable
ROOTPASS=default_root_pw

# setting answers to questions during install
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password $ROOTPASS'
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password $ROOTPASS'

sudo apt-get update
sudo apt-get -y install mysql-server-5.5 php5-mysql apache2 php5

# check if databasesetup file exists, if it doesn't, it means this is first time 
# and we should create user"

if [ ! -f /var/log/databasesetup ];
then
    echo "CREATE USER 'user'@'localhost' IDENTIFIED BY 'user_password'" | mysql -uroot -p$ROOTPASS
    echo "CREATE DATABASE my_database" | mysql -uroot -p$ROOTPASS
    echo "GRANT ALL ON my_database.* TO 'user'@'localhost'" | mysql -uroot -p$ROOTPASS
    echo "flush privileges" | mysql -uroot -p$ROOTPASS

    # create databasesetup file to prevent recreating users in future ..."
    touch /var/log/databasesetup

    # checking if initial.sql exists to restore it into VM database...

    if [ -f /vagrant/data/initial.sql ];
    then
        mysql -uroot -p$ROOTPASS my_database < /vagrant/data/initial.sql
    fi
fi

if [ ! -h /var/www ]; 
then 
    rm -rf /var/www sudo
    ln -s /vagrant/public /var/www

    service apache2 restart
fi

最佳答案

你有一个引用错误。在单引号内,$ROOTPASS只是一个静态字符串。 debconf-set-selections 中需要双引号片段。

无论如何,我会切换到此处的文档以避免代码重复。

sudo debconf-set-selections <<-____HERE
    mysql-server-5.5 mysql-server/root_password password $ROOTPASS
    mysql-server-5.5 mysql-server/root_password_again password $ROOTPASS
____HERE

(巧合的是,这也减少了线宽,以便您可以实际看到错误。当您的线过长时,体面的编辑会向您尖叫是有原因的。)

关于shell - 从 localhost 授予对 root 的访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161783/

相关文章:

ubuntu - 如何在较旧的 unix 上解压缩 7zip 文件?

linux - 如何通过 cron 按计划重新启动。乌类图14.04

chef-infra - 是否有相当于 Berkshelf 的产品,但用于 Puppet 模块?

linux - bash:当它是进程重定向的结果时执行 $0

linux - 将 dd 的输出读入 shell 脚本变量

c# - 如何编译和运行适用于 Linux 的 .NET Core 服务?

ubuntu - vagrant + ubuntu + services + 共享文件夹 (nfs) + boot

centos - memcached 无法在 CentOS 6.5 的 vagrant up 上启动

python - 如何用文件B中的第n行替换文件A中第n个出现的字符?

bash - 用于有效制表符完成的 Unix 文件命名约定?