CentOS7-Minimal安裝MySQL服務

CentOS7默認安裝的是Mariadb而不是mysql,而Mariadb是mysql的一個分支, 安裝mysql會覆蓋Mariadbhtml

1、下載MySQL官方的 Yum Repository

[root@localhost ~]# wget -i -c https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm 
--2018-07-26 10:21:52--  https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
正在解析主機 repo.mysql.com (repo.mysql.com)... 104.88.152.154
正在鏈接 repo.mysql.com (repo.mysql.com)|104.88.152.154|:443... 已鏈接。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:25548 (25K) [application/x-redhat-package-manager]
正在保存至: 「mysql57-community-release-el7-10.noarch.rpm」
 
100%[========================================================================================>] 25,548      36.5KB/s 用時 0.7s   
 
2018-07-26 10:21:58 (36.5 KB/s) - 已保存 「mysql57-community-release-el7-10.noarch.rpm」 [25548/25548])
 
-c: 沒有那個文件或目錄
在 -c 中找不到 URL。
FINISHED --2018-07-26 10:21:58--
Total wall clock time: 6.3s
Downloaded: 1 files, 25K in 0.7s (36.5 KB/s)
[root@localhost ~]#

2、安裝mysql57-community-release-el7-10.noarch.rpm包,安裝完以後會獲取mysql的yum repo源

[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
已加載插件:fastestmirror
正在檢查 mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch
mysql57-community-release-el7-10.noarch.rpm 將被安裝
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 mysql57-community-release.noarch.0.el7-10 將被 安裝
--> 解決依賴關係完成
 
依賴關係解決
 
==================================================================================================================================
 Package                             架構             版本               源                                                  大小
==================================================================================================================================
正在安裝:
 mysql57-community-release           noarch           el7-10             /mysql57-community-release-el7-10.noarch            30 k
 
事務概要
==================================================================================================================================
安裝  1 軟件包
 
總計:30 k
安裝大小:30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : mysql57-community-release-el7-10.noarch                                                                       1/1 
  驗證中      : mysql57-community-release-el7-10.noarch                                                                       1/1 
 
已安裝:
  mysql57-community-release.noarch 0:el7-10                                                                                       
 
完畢!
[root@localhost ~]# 
查看mysql的yum repo源:mysql-community.repo 和 mysql-community-source.repo

[root@localhost ~]# ls -l /etc/yum.repos.d/
總用量 40
-rw-r--r--. 1 root root 1664 4月  29 00:35 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 4月  29 00:35 CentOS-CR.repo
-rw-r--r--. 1 root root  649 4月  29 00:35 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 4月  29 00:35 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 4月  29 00:35 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 4月  29 00:35 CentOS-Sources.repo
-rw-r--r--. 1 root root 4768 4月  29 00:35 CentOS-Vault.repo
-rw-r--r--. 1 root root 1627 4月   5 2017 mysql-community.repo
-rw-r--r--. 1 root root 1663 4月   5 2017 mysql-community-source.repo
[root@localhost ~]#

3、使用yum安裝MySQL服務,此步驟會安裝mysql的依賴包,會浪費一些時間

[root@localhost ~]# yum -y install mysql-community-server
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.zju.edu.cn
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.shu.edu.cn
mysql-connectors-community                                                                                 | 2.5 kB  00:00:00     
mysql-tools-community                                                                                      | 2.5 kB  00:00:00     
mysql57-community                                                                                          | 2.5 kB  00:00:00     
(1/3): mysql-connectors-community/x86_64/primary_db                                                        |  20 kB  00:00:05     
(2/3): mysql-tools-community/x86_64/primary_db                                                             |  41 kB  00:00:06     
(3/3): mysql57-community/x86_64/primary_db                                                                 | 144 kB  00:00:06     
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 mysql-community-server.x86_64.0.5.7.22-1.el7 將被 安裝
--> 正在處理依賴關係 mysql-community-common(x86-64) = 5.7.22-1.el7,它被軟件包 mysql-community-server-5.7.22-1.el7.x86_64 須要
--> 正在處理依賴關係 mysql-community-client(x86-64) >= 5.7.9,它被軟件包 mysql-community-server-5.7.22-1.el7.x86_64 須要
.
.
.
---> 軟件包 perl-HTTP-Tiny.noarch.0.0.033-3.el7 將被 安裝
---> 軟件包 perl-parent.noarch.1.0.225-244.el7 將被 安裝
--> 解決依賴關係完成
 
依賴關係解決
 
==================================================================================================================================
 Package                                架構              版本                                 源                            大小
==================================================================================================================================
正在安裝:
 mysql-community-libs                   x86_64            5.7.22-1.el7                         mysql57-community            2.1 M
      替換  mariadb-libs.x86_64 1:5.5.56-2.el7
 mysql-community-libs-compat            x86_64            5.7.22-1.el7                         mysql57-community            2.0 M
      替換  mariadb-libs.x86_64 1:5.5.56-2.el7
 mysql-community-server                 x86_64            5.7.22-1.el7                         mysql57-community            165 M
爲依賴而安裝:
 mysql-community-client                 x86_64            5.7.22-1.el7                         mysql57-community             24 M
 mysql-community-common                 x86_64            5.7.22-1.el7                         mysql57-community            274 k
 net-tools                              x86_64            2.0-0.22.20131004git.el7             base                         305 k
 .
 .
 .
 perl-threads                           x86_64            1.87-4.el7                           base                          49 k
 perl-threads-shared                    x86_64            1.43-6.el7                           base                          39 k
 
事務概要
==================================================================================================================================
安裝  3 軟件包 (+30 依賴軟件包)
 
總下載量:205 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.22-1.el7.x86_64.rpm: 頭V3 DSA/SHA1 Signature, 密鑰 ID 5072e1f5: NOKEY
mysql-community-common-5.7.22-1.el7.x86_64.rpm 的公鑰還沒有安裝
(1/33): mysql-community-common-5.7.22-1.el7.x86_64.rpm                                                     | 274 kB  00:00:07     
(2/33): mysql-community-libs-5.7.22-1.el7.x86_64.rpm                                                       | 2.1 MB  00:01:00     
 .
 .
 .   
(32/33): mysql-community-client-5.7.22-1.el7.x86_64.rpm                                                    |  24 MB  00:11:56     
(33/33): mysql-community-server-5.7.22-1.el7.x86_64.rpm                                                    | 165 MB  01:05:31     
----------------------------------------------------------------------------------------------------------------------------------
總計                                                                                               52 kB/s | 205 MB  01:07:47     
從 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 檢索密鑰
導入 GPG key 0x5072E1F5:
 用戶ID     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 指紋       : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 軟件包     : mysql57-community-release-el7-10.noarch (installed)
 來自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : mysql-community-common-5.7.22-1.el7.x86_64                                                                   1/34 
  正在安裝    : mysql-community-libs-5.7.22-1.el7.x86_64                                                                     2/34 
  .
  .
  .  
  正在安裝    : mysql-community-libs-compat-5.7.22-1.el7.x86_64                                                             33/34 
  正在刪除    : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                          34/34 
  驗證中      : mysql-community-libs-compat-5.7.22-1.el7.x86_64                                                              1/34 
  驗證中      : perl-HTTP-Tiny-0.033-3.el7.noarch                                                                            2/34 
  .
  .
  .  
  驗證中      : perl-Text-ParseWords-3.29-4.el7.noarch                                                                      32/34 
  驗證中      : mysql-community-client-5.7.22-1.el7.x86_64                                                                  33/34 
  驗證中      : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                          34/34 
 
已安裝:
  mysql-community-libs.x86_64 0:5.7.22-1.el7                    mysql-community-libs-compat.x86_64 0:5.7.22-1.el7                 
  mysql-community-server.x86_64 0:5.7.22-1.el7                 
 
做爲依賴被安裝:
  mysql-community-client.x86_64 0:5.7.22-1.el7                    mysql-community-common.x86_64 0:5.7.22-1.el7                   
  net-tools.x86_64 0:2.0-0.22.20131004git.el7                     perl.x86_64 4:5.16.3-292.el7                                   
 .
 .
 .                         
  perl-threads.x86_64 0:1.87-4.el7                                perl-threads-shared.x86_64 0:1.43-6.el7                        
 
替代:
  mariadb-libs.x86_64 1:5.5.56-2.el7                                                                                              
 
完畢!
[root@localhost ~]#

4、啓動並查看Mysql狀態

[root@localhost ~]# systemctl start mysqld.service
[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2018-07-26 12:21:21 CST; 11s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 4113 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 4040 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 4116 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─4116 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
 
7月 26 12:21:06 localhost.localdomain systemd[1]: Starting MySQL Server...
7月 26 12:21:21 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]#

5、查看mysql初始化密碼

  • 這次mysql初始化密碼爲:1#(EdfAGebE5
  • 須要使用grep "password" /var/log/mysqld.log命令查看
[root@localhost ~]# grep "password" /var/log/mysqld.log
2018-07-26T04:21:09.246395Z 1 [Note] A temporary password is generated for root@localhost: 1#(EdfAGebE5
[root@localhost ~]#

6、登錄mysql,並輸入出廠密碼

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22
 
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> 
 

7、修改root用戶密碼

  • mysql強制用戶必須修改密碼,否則只能登錄,卻不能操做數據庫(注意:在修改密碼時密碼複雜度不能太簡單,如:123456,可是咱們本身開發時又不想設置的太複雜,因此在修改密碼以前先修改密碼複雜度,如若不想修改密碼複雜度,請直接執行ALTER語句)
# 密碼能夠只有一種字符
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
# 密碼最小長度爲1 
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'hadoop';
Query OK, 0 rows affected (0.00 sec)
 
mysql>

8、修改默認字符集

  • mysql默認的database和server字符集爲latin1,咱們須要把它修改成utf8
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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.01 sec)
 
mysql> set character_set_database=utf8;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)
 
mysql>

9、受權root用戶能夠從任意主機登陸(注意將hadoop換成本身的密碼)

mysql> grant all privileges on *.* to root@"%" identified by "hadoop" with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql>

10、退出並重啓mysql服務

mysql> quit
Bye
[root@localhost ~]# systemctl restart mysqld.service
[root@localhost ~]#

11、開放端口3306端口

[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]#
相關文章
相關標籤/搜索