从localhost授予对root用户的访问权限

时间:2014-11-27 01:42:12

标签: shell ubuntu vagrant

我关注如何使用Vagrant设置LAMP堆栈link

因为该链接使用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

1 个答案:

答案 0 :(得分:1)

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

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

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

(巧合的是,这也减少了线宽,因此您实际上可以看到错误。当你的线条长得太长时,有正当编辑会尖叫你的原因。)