MySql基礎-構建MySql數據庫:安裝MySql-server、MySql-client

1 構建MySQL服務器
1.1 問題

本案例要求熟悉MySQL官方安裝包的使用,快速構建一臺數據庫服務器:

安裝MySQL-server、MySQl-client軟件包
修改數據庫用戶root的密碼
確認MySQL服務程序運行、root可控

1.2 方案

本課程將使用64位的RHEL 7操作系統,MySQL數據庫的版本是5.7.17。

訪問http://dev.mysql.com/downloads/mysql/,找到MySQL Community Server下載頁面,平臺選擇“Red Hat Enterprise Linux 7/ Oracle Linux”,然後選擇64位的bundle整合包下載

注意:下載MySQL軟件時需要以Oracle網站賬戶登錄,如果沒有請根據頁面提示先註冊一個(免費)
1.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:準備工作

1)停止mariadb服務

[root@localhost ~]# systemctl stop mariadb

2)刪除/etc/my.cnf配置文件

此配置文件由RHEL自帶的mariadb-libs庫提供:

[root@localhost ~]# rm -rf /etc/my.cnf

3)刪除數據

[root@localhost ~]# rm -rf /var/lib/mysql/*

4)卸載軟件包(沒有會顯示未安裝軟件包)

[root@localhost ~]# rpm -e --nodeps mariadb-server mariadb `
警告:/var/log/mariadb/mariadb.log 已另存爲/var/log/mariadb/mariadb.log.rpmsave

步驟二:安裝軟件包

1)安裝mysql時可能會缺少某些依賴包,需提前單獨安裝

[root@localhost ~]# yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes

2)物理機傳輸解壓包給虛擬機192.168.4.1

[root@room9pc01 ~]# cd 桌面            
[root@room9pc01 桌面]# scp mysql-5.7.17.tar 192.168.4.1:/root/  //給虛擬機傳包
[email protected]'s password:
mysql-5.7.17.tar     100%  543MB  95.6MB/s   00:05

3)虛擬機192.168.4.1解壓mysql-5.7.17.tar 整合包

[root@localhost ~]# tar -xvf mysql-5.7.17.tar               //解壓mysql整合包
./mysql-community-client-5.7.17-1.el7.x86_64.rpm
./mysql-community-common-5.7.17-1.el7.x86_64.rpm
./mysql-community-devel-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
./mysql-community-libs-5.7.17-1.el7.x86_64.rpm
./mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
./mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
./mysql-community-server-5.7.17-1.el7.x86_64.rpm
./mysql-community-test-5.7.17-1.el7.x86_64.rpm

4)安裝

[root@localhost ~]# yum -y install perl-JSON
[root@localhost ~]# rm -rf mysql-community-server-minimal-5.7.17-1.el7.x86_64.rpm
[root@localhost ~]# rpm -Uvh mysql-community-*.rpm
[root@localhost ~]# rpm -qa |grep -i mysql

步驟三:啓動MySQL數據庫服務並設置開機自啓

[root@localhost ~]# systemctl start mysqld                  //啓動mysql服務
[root@localhost ~]# systemctl enable mysqld                 //設置開機自啓
[root@localhost ~]# systemctl status mysqld                 //查看mysql服務狀態
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2018-08-28 10:03:24 CST; 8min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 4284 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─4284 /usr/sbin/mysqld --daemonize --pid-file=/var/r...
8月 28 10:02:56 localhost.localdomain systemd[1]: Starting MySQ...
8月 28 10:03:24 localhost.localdomain systemd[1]: Started MySQL...
Hint: Some lines were ellipsized, use -l to show in full.

步驟四:連接MySQL服務器,修改密碼

查看隨機生成的root管理密碼

[root@localhost ~]#grep 'temporary password' /var/log/mysqld.log
2017-04-01T18:10:42.948679Z 1 [Note] A temporary password is generated for root@localhost: mtoa>Av<p6Yk        //隨機生成的管理密碼爲mtoa>Av<p6Yk

2)使用客戶端命令mysql連接到MySQL服務器

提示驗證時,填入前一步獲得的隨機密碼,驗證成功後即可進入“mysql> ”環境:

[root@localhost ~]# mysql -u root -p'mtoa>Av<p6Yk'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.17
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>                                     //登錄成功後,進入SQL操作環境

用該密碼登錄到服務端後,必須馬上修改密碼,不然會報如下錯誤:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

3)執行SET PASSWORD命令修改密碼

這個其實與validate_password_policy的值有關,默認爲1,所以剛開始設置的密碼必須符合長度,且必須含有數字,小寫或大寫字母,特殊字符。如果我們不希望密碼設置的那麼複雜,需要修改兩個全局參數:validate_password_policy與validate_password_length。validate_password_length默認值爲8,最小值爲4,如果你顯性指定validate_password_length的值小於4,儘管不會報錯,但validate_password_length的值將設爲4。

可參考下列指令:

mysql>set global validate_password_policy=0;      //只驗證長度
Query OK, 0 rows affected (0.00 sec)
mysql>set global validate_password_length=6;     //修改密碼長度,默認值是8個字符 
Query OK, 0 rows affected (0.00 sec)
mysql> alter user user() identified by "123456";  //修改登陸密碼
Query OK, 0 rows affected (0.00 sec)

上述操作的結果是——更改數據庫用戶root從本機訪問時的密碼,設爲123456。

退出“mysql> ”環境,重新登錄驗證,必須採用新的密碼才能登入:

mysql> exit                                  //退出 mysql> 環境
Bye
[root@localhost ~]# mysql -u root -p        //重新登錄
Enter password:                              //輸入新設置的密碼
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.17 MySQL Community Server (GPL)
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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章