laravel Vagrant SSH - 设置并连接到 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24855942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:50:22  来源:igfitidea点击:

Vagrant SSH - Setting up and connecting to MySQL

mysqllaravelsshvirtual-machinevagrant

提问by Vanessa

I recently installed VM and Vagrant to work with my Laravel projects. So I'm new to this. I dont have MySQL installed on my main machine. I tried to Vagrant upand then ssh [email protected] -p 2222. Everything is fine and dandy uptil this point. I want to access MySQL and cant get access. This is what ive done:

我最近安装了 VM 和 Vagrant 来处理我的 Laravel 项目。所以我是新手。我的主机上没有安装 MySQL。我试着Vagrant up然后ssh [email protected] -p 2222。一切都很好,花花公子直到这一点。我想访问 MySQL 并且无法访问。这就是我所做的:

mysql -u root -p (password "") -h localhost
mysql -u root -p (password "root") -h localhost
mysql -u root -h localhost

And even all of those without -h localhost. I havent set up MySQL before so keep in mind, ive never made an account to even access MySQL.

甚至所有那些没有-h localhost. 我之前没有设置过 MySQL,所以请记住,我从来没有创建过一个帐户来访问 MySQL。

Ive searched around and ive found this:

我四处搜索,发现了这个:

Go to:

去:

sudo vi /etc/mysql/my.cnf

And comment out: skip-external-lockingand change this: bind-address: 0.0.0.0.

并注释掉:skip-external-locking并更改此:bind-address: 0.0.0.0

I then went back and restarted MySQL using sudo service mysql restartand tried those commands again...still no access.

然后我返回并使用重新启动 MySQLsudo service mysql restart并再次尝试这些命令......仍然无法访问。

Btw the error im getting is: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

顺便说一句,我得到的错误是: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

However, there was something that i found interesting... I went to cd /etc/mysqland I found a file named: debian.cnfand it had a username and password and i tried that username and password and i was logged in!!! but i dont think i was supposed to do it that way. and that password provided by that file is probably a hash of something.

然而,我发现一些有趣的事情......我去了cd /etc/mysql,我找到了一个名为:的文件debian.cnf,它有一个用户名和密码,我尝试了该用户名和密码,然后我登录了!!!但我不认为我应该那样做。并且该文件提供的密码可能是某些东西的散列。

Im stuck. I dont know what to do from here!

我卡住了。我不知道从这里做什么!

UpdateI forgot to add: If i just provide mysql, I get this error ERROR 1045 (28000): Access denied for user 'vagrant'@'localhost' (using password: NO)

更新我忘了添加:如果我只是提供mysql,我会收到这个错误ERROR 1045 (28000): Access denied for user 'vagrant'@'localhost' (using password: NO)

回答by Nebez Briefkani

Considering you're working on a local vagrant server, I think it's okay to give this a try to rule out any other possible problems. Definitely don't do this on a production server, and ensure that your Vagrant machine is only accessible by you.

考虑到您正在本地 vagrant 服务器上工作,我认为可以尝试排除任何其他可能的问题。绝对不要在生产服务器上这样做,并确保您的 Vagrant 机器只能由您访问。

# fill in the blanks for root password, db name, username (local), and password
mysql -u root -p"rootpassword" -e "CREATE DATABASE db_name;
CREATE USER 'local'@'localhost' IDENTIFIED BY 'password';
CREATE USER 'local'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'local'@'localhost';
GRANT ALL PRIVILEGES ON * . * TO 'local'@'%';
FLUSH PRIVILEGES;"

# change bind address to allow connections from anywhere
sed -i "s/bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

# restart the sql service
sudo service mysql restart

回答by amrography

I run this

我运行这个

homestead up
homestead ssh
mysql -u homestead -psecret

for laravel 5.2 and default .env values

对于 laravel 5.2 和默认 .env 值

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret