从外部流浪者连接到MySQL

时间:2015-11-30 21:22:00

标签: mysql vagrant

我想将mycli连接到在vagrant实例中运行的MySQL服务器。

我的基本Vagrantfile看起来像以下代码片段:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"

  config.vm.box = "debian/wheezy64"

  config.vm.provision :shell, path: "misc/bootstrap.sh"
  config.vm.network "forwarded_port", guest: 80, host: 8082

  config.vm.synced_folder ".", "/var/www/",
    owner: "vagrant",
    group: "www-data",
    mount_options: ["dmode=775,fmode=664"],
    create: true

  config.vm.provider "virtualbox" do |vb|
      vb.memory = 1536
      vb.cpus = 1
  end
end

要安装必要的软件,我使用以下代码片段:

echo "mysql-server mysql-server/root_password password $DBPASSWD" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password $DBPASSWD" | debconf-set-selections

echo "phpmyadmin phpmyadmin/dbconfig-install boolean true" | debconf-set-selections
echo "phpmyadmin phpmyadmin/app-password-confirm password $DBPASSWD" | debconf-set-selections
echo "phpmyadmin phpmyadmin/mysql/admin-pass password $DBPASSWD" | debconf-set-selections
echo "phpmyadmin phpmyadmin/mysql/app-pass password $DBPASSWD" | debconf-set-selections
echo "phpmyadmin phpmyadmin/reconfigure-webserver multiselect none" | debconf-set-selections

sudo apt-get install -y apache2 libapache2-mod-php5 mysql-server php5-mysql php5 phpmyadmin php5-cli php5-curl vim
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/sites-available/phpmyadmin.conf
sudo ln -s /etc/apache2/sites-available/phpmyadmin.conf /etc/apache2/sites-enabled/phpmyadmin.conf

sudo a2enmod rewrite
sudo a2enmod php5
sudo cp /var/www/misc/apache2.conf /etc/apache2/sites-available/default

sudo service apache2 restart

mysql -uroot -p$DBPASSWD -e "CREATE USER '$DBUSER'@'localhost' IDENTIFIED BY '$DBPASSWD';GRANT ALL ON *.* TO '$DBUSER'@'localhost';FLUSH PRIVILEGES;CREATE DATABASE $DBNAME;"
mysql -u$DBUSER -p$DBPASSWD $DBNAME < /var/www/db.sql

我认为我的问题是我存储在数据库中的用户凭据。但我不知道答案。

2 个答案:

答案 0 :(得分:1)

如果从vm外部进行连接,则需要添加侦听主机的端口映射,并将请求转发给guest虚拟机。类似于为http定义的那个:

config.vm.network "forwarded_port", guest: 80, host: 8082

对于mysql(假设你的mysql侦听默认端口):

config.vm.network "forwarded_port", guest: 3360, host: 33600

之后,修改Vagrant文​​件vagrant reload

然后在连接时,指定端口

mysql -P 33600 -u user -p database

答案 1 :(得分:1)

除了JRD在端口转发上所说的,你需要确保mysql监听所有端口而不仅仅是本地

编辑/etc/mysql/my.cnf文件并确保

  • 您有bind-address = 0.0.0.0
  • 或您对该行#bind-address ...
  • 发表评论

确保在更改后重新启动mysql服务器

$ sudo service mysql restart

然后您可以从您的主机连接 - 如果您收到以下错误

$ mysql -h127.0.0.1 -P 33600 -uroot -p
Enter password:
ERROR 1130 (HY000): Host '172.16.42.2' is not allowed to connect to this MySQL server

然后回到客人身边

vagrant@precise64:~$ mysql -h127.0.0.1 -uroot -p
...
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.42.2' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

然后你应该没有问题从主机连接

$ mysql -h127.0.0.1 -P 33600 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.5.44-0ubuntu0.12.04.1 (Ubuntu)