从外部流浪者连接到MySQL [英] Connect to MySQL from outside vagrant
问题描述
我想用mycli连接到在无业游民的实例中运行的MySQL服务器.
I would like to connect with mycli to the MySQL server running inside a vagrant instance.
我的基本Vagrantfile看起来像下面的代码片段:
My basic Vagrantfile looks like the following code snippet:
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
要安装必要的软件,我使用以下代码片段:
To install the neccessary software I am using the following code snippet:
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
我认为我的问题是我存储在数据库中的用户凭证.但是我不知道答案.
I think my problem is the user credentials which I am storing into the database. But I couln't figure out answer.
推荐答案
,您需要确保mysql在所有端口上侦听,而不仅仅是在本地侦听.
on top of what JRD said on the port forwarding, you need to make sure mysql listens on all port and not just locally
编辑/etc/mysql/my.cnf
文件,并确保
- 您有
bind-address = 0.0.0.0
- 或者您在
#bind-address ...
行中添加注释
- you have
bind-address = 0.0.0.0
- or you comment the line
#bind-address ...
确保更改后重新启动mysql服务器
make sure to restart your mysql server after the change
$ sudo service mysql restart
然后您可以从主机进行连接-如果出现以下错误
Then you can connect from your host - In case you get the following error
$ 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)
那么您应该没有问题可以从主机连接
Then you should have no issue to connect from the host machine
$ 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)
这篇关于从外部流浪者连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!