linux中mysql如何远程连接

linux中mysql如何远程连接

两个步骤:

第一:让root允许远程登录

update user set host = '%' where user = 'root';

第二:给予root用户最大数据库权限

grant all privileges on *.* to 'root'@'%' identified by '123456';
flush privileges;

实操:

[root@bogon ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host, user from user;
+-----------+------+
| host      | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1       | root |
| bogon     |      |
| bogon     | root |
| localhost |      |
| localhost | root |
+-----------+------+
6 rows in set (0.01 sec)

mysql> update user set host = '%' where user = 'root';select host, user from user;
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
+-----------+------+
| host      | user |
+-----------+------+
| %         | root |
| 127.0.0.1 | root |
| ::1       | root |
| bogon     |      |
| bogon     | root |
| localhost |      |
+-----------+------+
6 rows in set (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

[root@bogon ~]# systemctl restart mysqld

[root@bogon ~]# systemctl status mysqld
● mysqld.service - MySQL Server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
     Active: active (running) since Wed 2025-09-17 19:50:47 CST; 1min 54s ago
       Docs: man:mysqld(8)
             https://dev.mysql.com/doc/refman/en/using-systemd.html
   Main PID: 1473 (mysqld)
      Tasks: 21 (limit: 10892)
     Memory: 439.8M
        CPU: 1.196s
     CGroup: /system.slice/mysqld.service
             └─1473 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note] InnoDB: 128 rollback segment(s) are active.
Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note] InnoDB: Waiting for purge to start
Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note] InnoDB: 5.6.40 started; log sequence number 1626087
Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note] Server hostname (bind-address): '*'; port: 3306
Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note] IPv6 is available.
Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note]   - '::' resolves to '::';
Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note] Server socket created on IP: '::'.
Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note] Event Scheduler: Loaded 0 events
Sep 17 19:50:48 bogon mysqld[1473]: 2025-09-17 19:50:48 1473 [Note] /application/mysql/bin/mysqld: ready for connections.
Sep 17 19:50:48 bogon mysqld[1473]: Version: '5.6.40'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)
[root@bogon ~]#

posted @ 2025-09-17 20:02  guixiang  阅读(50)  评论(0)    收藏  举报