如何在Linux中搭建mysql服務(wù)(Ubuntu)

安裝MySQL服務(wù)(配置編碼格式)

  • 安裝mysql服務(wù)
xiaodong@Xiaodong-VMSrv:~$ sudo apt install mysql-server
[sudo] password for xiaodong: 
Reading package lists... Done
......
Do you want to continue? [Y/n] y
......
Preconfiguring packages ...
......
Setting up libhtml-tagset-perl (3.20-2) 
......
Setting up mysql-server (5.7.17-0ubuntu0.16.04.2) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...
Processing triggers for systemd (229-4ubuntu16) ...
Processing triggers for ureadahead (0.100.0-19) ...
  • 查看MySQL的狀態(tài)
xiaodong@Xiaodong-VMSrv:~$ sudo service mysql status
[sudo] password for xiaodong: 
    ● mysql.service - MySQL Community Server
       Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
       Active: active (running) since Wed 2017-04-19 21:01:19 CST; 28min ago
     Main PID: 4151 (mysqld)
        Tasks: 28
       Memory: 110.6M
          CPU: 3.911s
       CGroup: /system.slice/mysql.service
               └─4151 /usr/sbin/mysqld
    
    Apr 19 21:01:17 Xiaodong-VMSrv systemd[1]: Starting MySQL Community Server...
    Apr 19 21:01:19 Xiaodong-VMSrv systemd[1]: Started MySQL Community Server.```
    
* 登錄

```shell
xiaodong@Xiaodong-VMSrv:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.17-0ubuntu0.16.04.2 (Ubuntu)
    
Copyright (c) 2000, 2016, 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> show databases;#查看數(shù)據(jù)庫(kù)
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)```
    
* 顯示編碼格式

```shell 
mysql> show VARIABLES LIKE '%char%'
    -> ;
+--------------------------+----------------------------+
    | Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.06 sec)
    
mysql> SHOW VARIABLES LIKE '%colla%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.07 sec)
  • 退出
mysql> exit
Bye
  • 逐級(jí)進(jìn)入配置文件
xiaodong@Xiaodong-VMSrv:~$ cd /etc/mysql
xiaodong@Xiaodong-VMSrv:/etc/mysql$ ls
conf.d  debian.cnf  debian-start  my.cnf  my.cnf.fallback  mysql.cnf  mysql.conf.d
xiaodong@Xiaodong-VMSrv:/etc/mysql$ 
  • 修改配置文件(編碼格式)
xiaodong@Xiaodong-VMSrv:/etc/mysql$ sudo nano my.cnf
[sudo] password for xiaodong: 
#修改配置文件(修改數(shù)據(jù)庫(kù)編碼格式)
[client]
default-character-set = utf8
    
[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
#保存退出

vim----命令(先按esc鍵-->輸入:wq保存修改并退出誉碴,輸入:q!不保存修改并退出編輯器)

  • 重啟mysql服務(wù)
xiaodong@Xiaodong-VMSrv:/etc/mysql$ sudo service mysql restart
xiaodong@Xiaodong-VMSrv:/etc/mysql$ mysql -uroot -p
Enter password: 
......
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  • 查看編碼格式是否修改正確
mysql> SHOW VARIABLES LIKE '%CHAR%'
    -> ;
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
#下面是查看mysql的排序規(guī)則
mysql> SHOW VARIABLES LIKE '%colla%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.10 sec)
  • 回到用戶目錄
xiaodong@Xiaodong-VMSrv:/etc/mysql$ cd```
* 查看IP地址

```shell
xiaodong@Xiaodong-VMSrv:~$ ifconfig
enp0s3    Link encap:Ethernet  HWaddr 08:00:27:69:ec:7a  
       inet addr:192.168.1.138  Bcast:192.168.1.255  Mask:255.255.255.0
       inet6 addr: fd7a:3e42:5e2f:0:a00:27ff:fe69:ec7a/64 Scope:Global
       inet6 addr: fe80::a00:27ff:fe69:ec7a/64 Scope:Link
       UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
       RX packets:17403 errors:0 dropped:0 overruns:0 frame:0
       TX packets:7494 errors:0 dropped:0 overruns:0 carrier:0
       collisions:0 txqueuelen:1000 
       RX bytes:21652498 (21.6 MB)  TX bytes:625769 (625.7 KB)
    
lo        Link encap:Local Loopback  
       inet addr:127.0.0.1  Mask:255.0.0.0
       inet6 addr: ::1/128 Scope:Host
       UP LOOPBACK RUNNING  MTU:65536  Metric:1
       RX packets:160 errors:0 dropped:0 overruns:0 frame:0
       TX packets:160 errors:0 dropped:0 overruns:0 carrier:0
       collisions:0 txqueuelen:1 
       RX bytes:11840 (11.8 KB)  TX bytes:11840 (11.8 KB)
ip地址----192.168.1.138```
* 創(chuàng)建一個(gè)用戶属提,使得通過(guò)這個(gè)用戶的所有外界主機(jī)能夠連接到服務(wù)器中的mysql(允許所有IP都能登錄的用戶)

```shell
mysql> create user 'xiaodong'@'%' IDENTIFIED BY 'xiaoxiao'
    -> ;
Query OK, 0 rows affected (0.00 sec)
    
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#為該用戶授權(quán)溉卓,便于后面的數(shù)據(jù)庫(kù)操作
mysql> grant all on *.* to 'xiaodong'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> 
  • 修改監(jiān)聽(tīng)地址
    • 默認(rèn)情況下MySQL只允許本機(jī)localhost(127.0.0.1)訪問(wèn)枣宫,有時(shí)為了開(kāi)發(fā)需求哮翘,我們需要讓MySQL監(jiān)聽(tīng)外來(lái)機(jī)器的請(qǐng)求锣杂。這樣也便于后期項(xiàng)目的部署。
#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
    
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
#修改后的配置文件
[client]
default-character-set = utf8

[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
bind-address = 0.0.0.0#添加監(jiān)聽(tīng)ip
  • 查看監(jiān)聽(tīng)詳情
xiaodong@Xiaodong-VMSrv:~$ sudo netstat -tap | grep mysql
tcp        0      0 *:mysql                 *:*                     LISTEN      2254/mysqld 
    
0  *:mysql-----指的所有外界主機(jī)ip都可連接    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末厂汗,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子黍翎,更是在濱河造成了極大的恐慌面徽,老刑警劉巖,帶你破解...
    沈念sama閱讀 210,978評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匣掸,死亡現(xiàn)場(chǎng)離奇詭異趟紊,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)碰酝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門霎匈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人送爸,你說(shuō)我怎么就攤上這事铛嘱。” “怎么了袭厂?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,623評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵墨吓,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我纹磺,道長(zhǎng)帖烘,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,324評(píng)論 1 282
  • 正文 為了忘掉前任橄杨,我火速辦了婚禮秘症,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘式矫。我一直安慰自己乡摹,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,390評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布采转。 她就那樣靜靜地躺著聪廉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪故慈。 梳的紋絲不亂的頭發(fā)上板熊,一...
    開(kāi)封第一講書(shū)人閱讀 49,741評(píng)論 1 289
  • 那天,我揣著相機(jī)與錄音惯悠,去河邊找鬼。 笑死竣况,一個(gè)胖子當(dāng)著我的面吹牛克婶,可吹牛的內(nèi)容都是我干的筒严。 我是一名探鬼主播,決...
    沈念sama閱讀 38,892評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼情萤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼鸭蛙!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起筋岛,我...
    開(kāi)封第一講書(shū)人閱讀 37,655評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤娶视,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后睁宰,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體肪获,經(jīng)...
    沈念sama閱讀 44,104評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評(píng)論 2 325
  • 正文 我和宋清朗相戀三年柒傻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了孝赫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,569評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡红符,死狀恐怖青柄,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情预侯,我是刑警寧澤致开,帶...
    沈念sama閱讀 34,254評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站萎馅,受9級(jí)特大地震影響双戳,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜校坑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,834評(píng)論 3 312
  • 文/蒙蒙 一拣技、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧耍目,春花似錦膏斤、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,725評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至毅访,卻和暖如春沮榜,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背喻粹。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,950評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工蟆融, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人守呜。 一個(gè)月前我還...
    沈念sama閱讀 46,260評(píng)論 2 360
  • 正文 我出身青樓型酥,卻偏偏與公主長(zhǎng)得像山憨,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子弥喉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,446評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容