1. 程式人生 > >(一)5、安裝與配置MySQL

(一)5、安裝與配置MySQL

ubun register oot local sock www sans res state

最後當然還有我們的MySQL啦(遇選擇請選“Y”)

root@ubuntu:/# sudo apt-get install mysql-server

漫長的等待之後,我的界面出現了非常sao的紫色,提示說輸入MySQL的root用戶的密碼,那就輸入吧,我的是**********,OK,再次輸入,OK

技術分享圖片

最後我們測試一下,MySQL到底安沒安上……完美

root@ubuntu:/# mysql -u root -p
Enter password:(此處輸入密碼)
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu) 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>

那就再看看有那些自帶的數據庫?完美,MySQL安裝也到此結束了!

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql>

退出MySQL

mysql>exit

接下來就是我們的配置MySQL

1、因為MySQL的默認端口是3306,所以我們需要確認3306端口是否打開

root@ubuntu:/# ufw status

技術分享圖片

然後果然沒打開,那就來打開吧

root@ubuntu:/# ufw allow 3306
Rule added
Rule added(v6)

最好檢查一遍

root@ubuntu:/# ufw status

技術分享圖片

好的,那就下一步吧!

2、接著我們來看下MySQL的運行狀態

root@ubuntu:/# netstat -antup

發現是127.0.0.1:3306,這可不行,因為127.0.0.1是本機IP,我們是需要開放出去的,那咱就來開放吧,進入mysql的配置文件(罪魁禍首就是紅色標出來的,把這行註釋掉就好了,即在這行前面加個#就可以註釋了),你TM告訴我不知道怎麽改??按鍵盤上的“insert”,還要不要我拿著你的手按方向鍵和刪除鍵啊

root@ubuntu:/# cd /etc/mysql/mysql.cnf.d
root@ubuntu:/#ls
root@ubuntu:/#vi mysqld.conf
……
[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
  bind-address            = 127.0.0.1
#
# * Fine Tuning
#
……

最後應該是這樣的,然後按“ESC”,輸入“:wq”保存退出就好了

root@ubuntu:/# cd /etc/mysql/mysql.conf.d
root@ubuntu:/etc/mysql/mysql.conf.d#ls
mysqld.cnf mysqld_safe_syslog.cnf
root@ubuntu:/#vi mysqld.cnf
……
[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# bind-address            = 127.0.0.1
#
# * Fine Tuning
#
……

真的是最後一步,想要讓配置生效,也要讓它重啟一次吧,不知道命令?這就來

root@ubuntu:/# service mysql restart

然後再來查看MySQL的運行狀態,能看到:::3306我就放心了,唉,現在心有點累了

root@ubuntu:/# netstat -antup

然後再來創建所需要的數據庫和簡單操作一下數據庫。

1、進入數據庫

root@ubuntu:/#mysql -u root -p
Enter password:(輸入密碼)
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection
id is 7 Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu) 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>

1、我們需要創建一個數據庫,來供後面的教程使用

mysql> create database example;(註意分號)
Query OK, 1 row affected (0.00 sec)

2、進入數據庫“example”

mysql> use example
Database changed

3、創建數據表

mysql> create table People(
    -> Id int,
    -> Name varchar(10),
    -> Age int,
    -> Gender varchar(5)
    -> );
Query OK, 0 rows affected (0.05 sec)

4、來看看我們的數據表的結構

mysql> desc People;                                                             
+--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | varchar(10) | YES | | NULL | | | Age | int(11) | YES | | NULL | | | Gender | varchar(5) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)

5、來看看我們數據表的數據,嗯…沒插入數據怎麽會有數據呢

mysql> select * from People;
Empty set (0.00 sec)

6、那咱就來試試如何插入數據吧

mysql> insert into People value(1,Tom,20,male);
Query OK, 1 row affected (0.00 sec)

7、看看我們剛剛插入的數據

mysql> select * from People;
+------+------+------+--------+
| Id   | Name | Age  | Gender |
+------+------+------+--------+
|    1 | Tom  |   20 | male   |
+------+------+------+--------+
1 row in set (0.00 sec)

完美,數據庫的操作就先到這了。接下來才是重頭戲。—授權

只要執行下面一句,就能創建一個用戶,並給該用戶授予有關權限

mysql> grant all privileges on *.* to myUser@%identified by 123456;
Query OK, 0 rows affected, 1 warning (0.00 sec)

解釋:

grant:授權關鍵字

all privileges:所有權限

*.*:所有數據庫的所有表

myUser:被授權的用戶名,即登錄的用戶名

%:表示所有主機都可以訪問

‘123456‘:被授權的用戶名的密碼,即登錄的密碼

最後退出MySQL

mysql>exit
Bye

至此,(一)的任務已經完成了,下面就是(二)的時間了,首頁傳送門

(一)5、安裝與配置MySQL