dongshen9686 2017-02-23 17:43
浏览 119
已采纳

如何允许远程mysql登录?

I have simple vagrant multi-machine config:

Vagrant.configure(2) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.provision :shell, path: "vagrant/ubuntu-16.04/provision.sh"
  config.vm.synced_folder "ed", "/var/www/html"
  config.vm.provider "virtualbox" do |v|
    v.memory = 1024
    v.cpus = 2
  end
  config.vm.define "node1", primary: true do |node|
    node.vm.hostname = 'node1'
    node.vm.network :private_network, ip: "192.168.56.101"
    node.vm.network :forwarded_port, guest: 22, host: 10122, id: "ssh"
    node.vm.provision :hosts, :sync_hosts => true
  end
  config.vm.define "node2" do |node|
    node.vm.hostname = "node2"
    node.vm.network :private_network, ip: "192.168.56.102"
    node.vm.network :forwarded_port, guest: 22, host: 10123, id: "ssh"
    node.vm.provision :hosts, :sync_hosts => true
  end
end

On node1 I have php and on node2 I have mysql.
When I run mysql -uuser -ppass -P3306 -hlocalhost from vagrant@node2 it works,
but when I try run mysql -uuser -ppass -P3306 -h192.168.56.102 from vagrant@node1 it won't work, I get error:

ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.56.102' (111)

I can ping node1 from node2 and vice versa.

  • 写回答

2条回答 默认 最新

  • doubi1797 2017-02-24 13:51
    关注

    In mysql, remote user login is disabled by default. It only allow localhost for security reason.

    For cross verification, login to mysql and run command:

    mysql> select user,host from mysql.user;
    +------+--------------------------------+
    | user | host                           |
    +------+--------------------------------+
    | root | localhost                      | 
    | root | ::1                            |
    +------+--------------------------------+
    2 rows in set (0.00 sec)
    

    If it is giving localhost only. it means you are only allowed to login from localhost and user as root(in this example).

    If you want to allow login from remote system then you have to add like:

    GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
    

    This command will allow login from all remote IP and give access to all database to username user.

    If you want to allow from specific IP address and allow access only specific database then you can run like:

    GRANT ALL PRIVILEGES ON databasename.* TO 'user'@'ipaddress' IDENTIFIED BY 'password' WITH GRANT OPTION;
    

    Note: Don't forget to change bind-address in /etc/mysql/my.cnf.

    Open file /etc/mysql/my.cnf and change bind-address from 127.0.0.1 to 0.0.0.0 or comment out bind-address and restart mysql service.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile