Access mysql remote database from command line

后端 未结 17 1554
忘掉有多难
忘掉有多难 2020-11-28 01:41

I have a server with Rackspace. I want to access the database from my local machine command line.

I tried like:

mysql -u username -h my.application.com         


        
17条回答
  •  清酒与你
    2020-11-28 02:11

    If you want to not use ssh tunnel, in my.cnf or mysqld.cnf you must change 127.0.0.1 with your local ip address (192.168.1.100) in order to have access over the Lan. example bellow:

    sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
    

    Search for bind-address in my.cnf or mysqld.cnf

    bind-address            =  127.0.0.1
    

    and change 127.0.0.1 to 192.168.1.100 ( local ip address )

    bind-address            =  192.168.1.100
    

    To apply the change you made, must restart mysql server using next command.

    sudo /etc/init.d/mysql restart
    

    Modify user root for lan acces ( run the query's bellow in remote server that you want to have access )

    root@192.168.1.100:~$ mysql -u root -p
    

    ..

    CREATE USER 'root'@'%' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    

    If you want to have access only from specific ip address , change 'root'@'%' to 'root'@'( ip address or hostname)'

    CREATE USER 'root'@'192.168.1.100' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.100' WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    

    Then you can connect:

    nobus@xray:~$ mysql -h 192.168.1.100 -u root -p
    

    tested on ubuntu 18.04 server

提交回复
热议问题