Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant SSH - Setting up and connecting to MySQL

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 up and 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:

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.

Ive searched around and ive found this:

Go to:

sudo vi /etc/mysql/my.cnf

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

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

Btw the error im getting is: 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/mysql and I found a file named: debian.cnf and 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.

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

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

like image 232
Vanessa Avatar asked Jul 20 '14 23:07

Vanessa


People also ask

How do I connect to a remote MySQL server using SSH?

Open a new terminal window. Direct your local MySQL client to 127.0. 0.1:3306 with the MySQL server username and password. Your connection to the remote MySQL server will be encrypted through SSH, allowing you to access your databases without running MySQL on a public IP.

How do I connect my vagrant box to SSH?

Simply CMD-SHIFT-P then "Remote-SSH: Connect to Host..." and the ssh . config entry you just added is automatically listed - you simply select it and voila, vscode connects to your remote vagrant vm!


1 Answers

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.

# 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
like image 117
Nebez Briefkani Avatar answered Oct 06 '22 03:10

Nebez Briefkani