標籤:ref har user ping 教程 current 設定檔 database name
最後當然還有我們的MySQL啦(遇選擇請選“Y”)
[email protected]:/# sudo apt-get install mysql-server
漫長的等待之後,我的介面出現了非常sao的紫色,提示說輸入MySQL的root使用者的密碼,那就輸入吧,我的是**********,OK,再次輸入,OK
最後我們測試一下,MySQL到底安沒安上……完美
[email protected]:/# mysql -u root -pEnter password:(此處輸入密碼)
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 6Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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連接埠是否開啟
[email protected]:/# ufw status
然後果然沒開啟,那就來開啟吧
[email protected]:/# ufw allow 3306
Rule added
Rule added(v6)
最好檢查一遍
[email protected]:/# ufw status
好的,那就下一步吧!
2、接著我們來看下MySQL的運行狀態
[email protected]:/# netstat -antup
發現是127.0.0.1:3306,這可不行,因為127.0.0.1是本機IP,我們是需要開放出去的,那咱就來開放吧,進入mysql的設定檔(罪魁禍首就是紅色標出來的,把這行注釋掉就好了,即在這行前面加個#就可以注釋了),你TM告訴我不知道怎麼改??按鍵盤上的“insert”,還要不要我拿著你的手按方向鍵和刪除鍵啊
[email protected]:/# cd /etc/mysql/mysql.cnf.d[email protected]:/#ls[email protected]:/#vi mysqld.conf……[mysqld]## * Basic Settings#user = mysqlpid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockport = 3306basedir = /usrdatadir = /var/lib/mysqltmpdir = /tmplc-messages-dir = /usr/share/mysqlskip-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”儲存退出就好了
[email protected]:/# cd /etc/mysql/mysql.conf.d
[email protected]:/etc/mysql/mysql.conf.d#ls
mysqld.cnf mysqld_safe_syslog.cnf
[email protected]:/#vi mysqld.cnf
……[mysqld]## * Basic Settings#user = mysqlpid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockport = 3306basedir = /usrdatadir = /var/lib/mysqltmpdir = /tmplc-messages-dir = /usr/share/mysqlskip-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#……
真的是最後一步,想要讓配置生效,也要讓它重啟一次吧,不知道命令?這就來
[email protected]:/# service mysql restart
然後再來查看MySQL的運行狀態,能看到:::3306我就放心了,唉,現在心有點累了
[email protected]:/# netstat -antup
然後再來建立所需要的資料庫和簡單操作一下資料庫。
1、進入資料庫
[email protected]:/#mysql -u root -pEnter password:(輸入密碼)
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 7Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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 exampleDatabase 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 [email protected]‘%‘identified by ‘123456‘;Query OK, 0 rows affected, 1 warning (0.00 sec)
解釋:
grant:授權關鍵字
all privileges:所有許可權
*.*:所有資料庫的所有表
myUser:被授權的使用者名稱,即登入的使用者名稱
%:表示所有主機都可以訪問
‘123456‘:被授權的使用者名稱的密碼,即登入的密碼
最後退出MySQL
mysql>exit
Bye
至此,(一)的任務已經完成了,下面就是(二)的時間了,首頁傳送門
(一)5、安裝與配置MySQL