MySQL的安裝與連線方法

2021-08-20 04:40:26 字數 3583 閱讀 7062

mysql 是乙個關係型資料庫管理系統(關聯式資料庫將資料儲存在不同的表中,而不是將所有資料放在乙個大倉庫內,這樣就增加了速度並提高了靈活性),由瑞典mysql ab公司開發,目前屬於 oracle 旗下產品。

mysql 所使用的 sql 語言是用於訪問資料庫的、最常用的標準化語言。

本文介紹 mysql 在 centos 7 上的安裝方法。

利用 mysql 的 yum 倉庫,我們可以使用 yum 命令簡便地安裝/更新 mysql 。在這裡介紹使用 yum 方式安裝 mysql 的方法。

wget
rpm -ivh mysql-community-release-el7-5.noarch.rpm
此時,在系統的 yum 倉庫目錄下,檢視 mysql 的倉庫檔案是否已經新增了,如下:

根據查詢結果,能夠看到 mysql 的 yum 倉庫已經新增了。

使用 yum 命令安裝 mysql ,如下:

yum install mysql-server -y
systemctl start mysqld
預設情況下,mysql 將會繫結到 localhost (127.0.0.1) 。

mysql 的標準客戶端工具會隨著 mysql-server 包一起安裝,這個 mysql 客戶端需要通過終端連線 mysql 伺服器。如下:

[root@node1 /opt/liitdar/mydemos]# mysql
執行上述命令後,觀察 mysql 客戶端是否成功連線到了 mysql 伺服器,如下:

上述結果說明 mysql 安裝成功了。

預設情況下, mysql 會繫結到 localhost (127.0.0.1) ,即只允許本機連線 mysql(即本文前面的連線方式)。

這裡將介紹通過修改user表的方式,允許遠端連線資料庫的方法。

mysql 資料庫中缺省會有乙個名為「mysql」的資料庫,在該資料庫中,有乙個名為「user」的資料表,該錶會控制mysql伺服器的外部連線許可權(資訊),如下:

mysql> select host,user,password from user;

+-----------+------+----------+

| host | user | password |

+-----------+------+----------+

| node1 | root | |

| 127.0.0.1 | root | |

| ::1 | root | |

| localhost | | |

| node1 | | |

+-----------+------+----------+

5 rows in set (0.00 sec)

mysql>

在 user 表中,存在「host」、「user」和「password」字段,這些欄位即控制著能夠連線到mysql伺服器的「主機名」、「使用者名稱」和「密碼」。所以,為了能讓所有的外部主機訪問到這個mysql資料庫,需要進行以下操作:

1. 我們更新「user」表的 host 欄位的值,如下:

mysql> update user set host = '%' where user = 'root' and host = '127.0.0.1';

query ok, 1 row affected (0.00 sec)

rows matched: 1 changed: 1 warnings: 0

mysql>

說明:我們將host為「127.0.0.1」的記錄改為了host = '%'。host 欄位的值為「%」、user 欄位的值為「root」表示在任何客戶端機器上都能以 root 使用者身份登入到mysql伺服器上。建議在軟體開發過程中將 host 的值設為「%」。

2. 執行完上述操作後,查詢一下更新後的 user 表,如下:

mysql> select host,user,password from user;

+-----------+------+----------+

| host | user | password |

+-----------+------+----------+

| node1 | root | |

| % | root | |

| ::1 | root | |

| localhost | | |

| node1 | | |

+-----------+------+----------+

5 rows in set (0.00 sec)

mysql>

在上述查詢結果能夠看到,更新的內容已經寫入到 user 表中了。

3. 此時,我們需要把此設定(更新mysql伺服器的訪問許可權)推送到記憶體中(或重啟mysql伺服器),這樣外部客戶端才能訪問mysql伺服器。

推送設定到內容中的命令如下:

mysql> flush privileges;

query ok, 0 rows affected (0.00 sec)

mysql>

4. 完成上述操作後,我們就可以通過外部客戶端(如192.168.213.129)訪問mysql資料庫(192.168.213.128)了,如下:

[root@node2 ~]# mysql -h 192.168.213.128 -u root

welcome to the mysql monitor. commands end with ; or \g.

your mysql connection id is 8

server version: 5.6.40 mysql community server (gpl)

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>

安裝python與mysql連線的模組

python標準庫中沒有和mysql連線的庫,如果自己不單獨寫與mysql連線的庫,就需要另外去安裝mysqldb這個模組 mysql python 1.2.3.tar.gz tar xf mysql python 1.2.3.tar.gz cd mysql python 1.2.3 yum ins...

mysql安裝 連線

開啟後進入mysql的bin目錄,執行 mysqld initialize console 生成密碼 2022 03 04t08 45 54.013764z 6 note my 010454 server a temporary password is generated for root loca...

mysql安裝連線錯誤 MySQL連線出錯及解決

1.安裝完系統自帶的mysql後連線資料庫 mysql u root error2002 can t connect to local mysql server through socket var lib mysql mysql.sock 2 連線時出現如上錯誤提示導致連線資料庫失敗!2.此時檢視...