vs code 連接數據庫


數據庫允許遠程登錄

連接數據庫

[root@localhost ~]# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

數據庫未啟用,啟用數據庫


[root@localhost ~]# ss -tunlp
Netid  State    Recv-Q   Send-Q             Local Address:Port       Peer Address:Port                                               
udp    UNCONN   0        0           192.168.208.246%eth0:68              0.0.0.0:*       users:(("NetworkManager",pid=947,fd=20))   
udp    UNCONN   0        0                      127.0.0.1:323             0.0.0.0:*       users:(("chronyd",pid=916,fd=6))           
udp    UNCONN   0        0                          [::1]:323                [::]:*       users:(("chronyd",pid=916,fd=7))           
tcp    LISTEN   0        128                    127.0.0.1:45233           0.0.0.0:*       users:(("node",pid=1900,fd=18))            
tcp    LISTEN   0        128                      0.0.0.0:22              0.0.0.0:*       users:(("sshd",pid=956,fd=5))              
tcp    LISTEN   0        128                         [::]:22                 [::]:*       users:(("sshd",pid=956,fd=7))   

3306端口不存在,可以啟用


[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# ss -tunlp
Netid  State    Recv-Q   Send-Q             Local Address:Port       Peer Address:Port                                               
udp    UNCONN   0        0           192.168.208.246%eth0:68              0.0.0.0:*       users:(("NetworkManager",pid=947,fd=20))   
udp    UNCONN   0        0                      127.0.0.1:323             0.0.0.0:*       users:(("chronyd",pid=916,fd=6))           
udp    UNCONN   0        0                          [::1]:323                [::]:*       users:(("chronyd",pid=916,fd=7))           
tcp    LISTEN   0        128                    127.0.0.1:45233           0.0.0.0:*       users:(("node",pid=1900,fd=18))            
tcp    LISTEN   0        128                      0.0.0.0:22              0.0.0.0:*       users:(("sshd",pid=956,fd=5))              
tcp    LISTEN   0        128                         [::]:22                 [::]:*       users:(("sshd",pid=956,fd=7))              
tcp    LISTEN   0        80                             *:3306                  *:*       users:(("mysqld",pid=3512,fd=20))  

數據庫啟用成功,連接數據庫


[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

連接成功


設置允許遠程連接

MariaDB [(none)]> show databases;

查看庫


MariaDB [(none)]> use mysql;

切換數據庫,使用另一個數據庫。 將數據庫名稱作為參數。


MariaDB [mysql]> show tables;

查看表


MariaDB [mysql]> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 127.0.0.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| ::1       | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| %         | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| %         | gogs | *461C3A96BAEF66E327658B3AFF86F8D71BEE3F99 |
+-----------+------+-------------------------------------------+
5 rows in set (0.000 sec)

查看user表的三個字段,% 允許所有地址對應的密碼忘記,刪除所有允許所有地址登錄


MariaDB [mysql]> delete from user where host='%';
Query OK, 2 rows affected (0.000 sec)

MariaDB [mysql]> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 127.0.0.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| ::1       | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-----------+------+-------------------------------------------+
3 rows in set (0.000 sec)

刪除成功


MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'root' WITH GRANT OPTION;
Query OK, 0 rows affected (0.000 sec)

MariaDB [mysql]> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 127.0.0.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| ::1       | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| %         | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-----------+------+-------------------------------------------+
4 rows in set (0.000 sec)

MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)

all privileges:表示將所有權限授予給用戶。也可指定具體的權限,如:SELECT、CREATE、DROP等。

on:表示這些權限對哪些數據庫和表生效,格式:數據庫名.表名,這里寫“*”表示所有數據庫,所有表。如果我要指定將權限應用到test庫的user表中,可以這么寫:test.user

to:將權限授予哪個用戶。格式:”用戶名”@”登錄IP或域名”。%表示沒有限制,在任何主機都可以登錄。比如:”yangxin”@”192.168.0.%”,表示yangxin這個用戶只能在192.168.0IP段登錄

identified by:指定用戶的登錄密碼

with grant option:表示允許用戶將自己的權限授權給其它用戶

FLUSH PRIVILEGES: 更新表


關閉防火牆,防火牆會攔截 mariadb客戶端連接3306端口

systemctl stop firewalld

vs code連接數據庫

安裝插件

  1. MySQL syntax
    1. MySQL語法高亮顯示支持
  2. mysql
    1. MySQL管理工具

插件安裝成功后進行數據庫連接


按順序輸入完主機名、用戶名、密碼、端口號,最后一項省略 之后左側連接成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM