1. 程式人生 > >centos 7.3 安裝 mysql-5.7.22-linux-glibc2.5-x86_64

centos 7.3 安裝 mysql-5.7.22-linux-glibc2.5-x86_64

1、下載:

[[email protected] ~]#  wget  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.5-x86_64.tar.gz 

2、解壓 :

tar  zxvf mysql-5.7.22-linux-glibc2.5-x86_64.tar.gz -C /usr/local  --解壓到指定目錄 然後再進行重新命名 為mysql

3、建立mysql 賬戶 

[[email protected]

~]#useradd -M -s /sbin/nologin  mysql   -- M 不建立使用者目錄

                                       或者  groupadd mysql

                                                 useradd -g mysql mysql

[[email protected] ~]# mkdir -p /data/mysqldata

[[email protected] ~]# chown  -R mysql:mysql /data/mysqldata  --將mysqldata所屬主所屬組賦予mysql使用者許可權

4、進行初始化:

[[email protected] ~]#cd /usr/local/mysql5.7.22

[[email protected] ~]#./bin/mysqld  --initialize --user=mysql --datadir= /data/mysqldata  

注意,這一步最後一行會有一個提示
[Note] A temporary password is generated for [email protected]: -gLjbSisv1uh

5、拷貝配置檔案和啟動指令碼

修改 /etc/my.cnf 檔案。

vim /etc/my.cnf //編輯或者修改

[mysqld]   --在這裡修改
basedir = /usr/local/mysql
datadir = /data/mysqldata
port = 3306
socket = /tmp/mysql.sock     -注意路徑不能出錯,否則啟動出錯 

 

cp support-files/mysql.server /etc/init.d/mysqld   --複製啟動指令碼到/etc/init.d下
vi /etc/init.d/mysqld  --編輯或者修改
basedir=/usr/local/mysql
datadir=/data/mysqldata

 

6. 啟動服務
/etc/init.d/mysqld start

加入開機啟動
chkconfig --add mysqld
chkconfig mysqld on

或者

mysqld --defaults-file=/usr/local/mysql5.7.22/my.cnf --user=mysql &

7、設定root密碼

注:編輯  vi /etc/profile  配置環境變數
export PATH=$PATH:/usr/local/mysql5.7.22/bin

# ln -s /usr/local/mysql/bin/mysql /usr/bin 對映一個連結到/usr/bin目錄下,建立一個連結檔案。

mysql -uroot -p/進入mysql (密碼為剛剛安裝完成密碼-gLjbSisv1uh)

成功登陸後 ,在輸入show databases 會提示你重新設定密碼,

mysql>set password = password(‘mypass‘); 退出來,再使用新的密碼登入就可以了、

2>.還有一種情況,就是不知道初始化密碼
vi /etc/my.cnf
在[mysqld]下面增加一行
skip-grant-tables
重啟  /etc/init.d/mysqld restart

 mysql -uroot 

mysql> update user set authentication_string=password(‘123333‘) where user=‘root‘;
退出來後,更改my.cnf,去掉剛加的 skip-grant-tables
重啟 /etc/init.d/mysqld restart
此時就可以使用新的密碼了。

此時安裝完成mysql 5.7

 

Mysql與Mariadb

    Mysql是一個關係型資料庫,由mysql ab公司開發,在2008年被sun公司收購,2009年被oracle公司收購。mysql最新版本是5.7GA/8.0DMR。mysql 5.6版本變化比較大,5.7版本效能上有很大提升。

    Mariadb是mysql的一個分支,最新版本是10.2版本。它是由Mysql原作者帶領大部分原班人馬創立的SkySQL公司維護。

    Mariadb5.5版本對應MySQL的5.5版本,10.0版本對應MySQL的5.6版本。

Tips:

Community 社群版本,Enterprise 企業版,

GA(Generally Available)指通用版本,在生產環境中用的,

DMR(Development Milestone Release)開發里程碑釋出版,

RC(Release Candidate)發行候選版本,Beta開放測試版本,Alpha內部測試版本。

 

安裝Mariadb

1、下載安裝檔案

[[email protected] src]# wget https://downloads.mariadb.com/MariaDB/mariadb-10.2.6/bintar-linux-glibc_214-x86_64/mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz

--2017-07-21 07:01:06--  https://downloads.mariadb.com/MariaDB/mariadb-10.2.6/bi             ntar-linux-glibc_214-x86_64/mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz

正在解析主機 downloads.mariadb.com (downloads.mariadb.com)... 51.255.94.155, 200             1:41d0:1004:249b::

正在連線 downloads.mariadb.com (downloads.mariadb.com)|51.255.94.155|:443... 已             連線。

已發出 HTTP 請求,正在等待迴應... 200 OK

長度:541295045 (516M) [application/octet-stream]

正在儲存至: “mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz”

 

5% [====>                                                                              7% [==>                                          ] 38,395,559  62.6KB/s 剩餘 1h 40m

2、解壓並移動

[[email protected] src]# tar zxvfmariadb-10.2.6-linux-glibc_214-x86_64.tar.gz

......

[[email protected] src]# mv mariadb-10.2.6-linux-glibc_214-x86_64 /usr/local/mariadb

3、建立使用者

[[email protected] mariadb]# useradd mysql

4、初始化資料庫

[[email protected] src]# cd /usr/local/mariadb

[[email protected] mariadb]# ls

bin                 data               include         mysql-test    share

COPYING             DESTINATION        INSTALL-BINARY  README.md     sql-bench

COPYING.thirdparty  docs               lib             README-wsrep  support-files

CREDITS             EXCEPTIONS-CLIENT  man             scripts

[[email protected] mariadb]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mariadb

Installing MariaDB/MySQL system tables in ‘/data/mariadb‘ ...

OK

 

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

 

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !

To do so, start the server, then issue the following commands:

 

‘./bin/mysqladmin‘ -u root password ‘new-password‘

‘./bin/mysqladmin‘ -u root -h server02 password ‘new-password‘

 

Alternatively you can run:

‘./bin/mysql_secure_installation‘

 

which will also give you the option of removing the test

databases and anonymous user created by default.  This is

strongly recommended for production servers.

 

See the MariaDB Knowledgebase at http://mariadb.com/kb or the

MySQL manual for more instructions.

 

You can start the MariaDB daemon with:

cd ‘.‘ ; ./bin/mysqld_safe --datadir=‘/data/mariadb‘

 

You can test the MariaDB daemon with mysql-test-run.pl

cd ‘./mysql-test‘ ; perl mysql-test-run.pl

 

Please report any problems at http://mariadb.org/jira

 

The latest information about MariaDB is available at http://mariadb.org/.

You can find additional information about the MySQL part at:

http://dev.mysql.com

[[email protected] mariadb]# echo $?

0

初始化顯示“OK”和“echo $?”校驗都表明了已正常初始化。

5、拷貝配置檔案

[[email protected] mariadb]# cp support-files/my-small.cnf /usr/local/mariadb/my.cnf

[[email protected] mariadb]# vi /usr/local/mariadb/my.cnf

......

basedir         =/usr/local/mariadb

datadir         =/data/mariadb

......

是選用“my-small.conf”還是“my-huge.cnf ”等這種檔案和裝置的配置有關,根據實際裝置配置選擇。

vi /etc/profile.d

export  PATH=/usr/local/mysql/bin:$PATH

source /etc/profile  使這個配置檔案及時生效

6、拷貝啟動指令碼檔案

[[email protected] mariadb]# cp support-files/mysql.server /etc/init.d/mariadb

[[email protected] mariadb]# vi /etc/init.d/mariadb

......

basedir=/usr/local/mariadb      ##定義basedir

datadir=/data/mariadb           ##定義datadir

conf=$basedir/my.cnf            ##定義conf

......

    then

      # Give extra arguments to mysqld with the my.cnf file. This script

      # may be overwritten at next upgrade.

      $bindir/mysqld_safe --defaults-file="$conf"--datadir="$datadir" --pid-file="$mysqld_pid_file_path" "[email protected]" &        ##增加defaults-file啟動引數

      wait_for_ready; return_value=$?

......

7、啟動Mysql

[[email protected] mariadb]# chkconfig --add mariadb

[[email protected] mariadb]# chkconfig --list

 

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置資料可能被原生 systemd 配置覆蓋。

      如果您想列出 systemd 服務,請執行 ‘systemctl list-unit-files‘。

      欲檢視對特定 target 啟用的服務請執行

      ‘systemctl list-dependencies [target]‘。

 

mariadb         0:關     1:關     2:開     3:開     4:開     5:開     6:關

netconsole      0:關     1:關     2:關     3:關     4:關     5:關     6:關

network         0:關     1:關     2:開     3:開     4:開     5:開     6:關

[[email protected] mariadb]# /etc/init.d/mariadb start

Reloading systemd:                                         [  確定  ]

Starting mariadb (via systemctl):  Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

                                                           [失敗]

[[email protected] mariadb]# systemctl status mariadb.service

● mariadb.service - LSB: start and stop MySQL

   Loaded: loaded (/etc/rc.d/init.d/mariadb; bad; vendor preset: disabled)

   Active: failed (Result: exit-code) since 五 2017-07-21 08:13:42 CST; 35s ago

     Docs: man:systemd-sysv-generator(8)

  Process: 10559 ExecStart=/etc/rc.d/init.d/mariadb start (code=exited, status=1/FAILURE)

 

7月 21 08:13:41 juispan mariadb[10559]: Fatal error in defaults handling. Program aborted

7月 21 08:13:41 juispan mariadb[10559]: WARNING: Defaults file ‘/usr/local/mariadb/my.cnf--datadir=/var/lib/mysql‘ not found!

7月 21 08:13:41 juispan mariadb[10559]: 170721 08:13:41 mysqld_safe Logging to ‘/usr/local/mysql/data/juispan.err‘.

7月 21 08:13:41 juispan mariadb[10559]: 170721 08:13:41 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

7月 21 08:13:41 juispan mariadb[10559]: [147B blob data]

7月 21 08:13:42 juispan mariadb[10559]: ERROR!

7月 21 08:13:42 juispan systemd[1]: mariadb.service: control process exited, code=exited status=1

7月 21 08:13:42 juispan systemd[1]: Failed to start LSB: start and stop MySQL.

7月 21 08:13:42 juispan systemd[1]: Unit mariadb.service entered failed state.

7月 21 08:13:42 juispan systemd[1]: mariadb.service failed.

發現“ Defaults file ‘/usr/local/mariadb/my.cnf--datadir=/var/lib/mysql‘ not found!”錯誤。檢視啟動指令碼,發現定義defaults-file的時候,兩個引數之間沒有空格導致。

[[email protected] mariadb]# /etc/init.d/mariadb start

Starting mariadb (via systemctl):                          [  確定  ]

[[email protected] mariadb]# netstat -lnpt

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address    Foreign Address         State       PID/Program name

tcp        0      0 0.0.0.0:22       0.0.0.0:*               LISTEN      1337/ssh

tcp        0      0 127.0.0.1:25     0.0.0.0:*               LISTEN      1863/master    

tcp        0      0 0.0.0.0:873      0.0.0.0:*               LISTEN      2368/rsync     

tcp6       0      0 :::3306          :::*                    LISTEN      3307/mysqld    

tcp6       0      0 :::22            :::*                    LISTEN      1337/sshd      

tcp6       0      0 ::1:25           :::*                    LISTEN      1863/master    

tcp6       0      0 :::873           :::*                    LISTEN      2368/rsync

 

綜上所述:mariadb的安裝簡直和mysql一模一樣。如果伺服器上面只有mariadb這個服務,我們完全可以把my.cnf放在etc下,同時也就不需要定義conf以及變量了。

 

 

Mariadb二進位制安裝和配置說明

 

相關軟體包介紹

mariadb-10.2.11.tar.gz   原始碼包,編譯用的

Galera 25.3.22   搞mariadb叢集用的,單機不需要

 

mariadb-10.2.11-winx64.msi

mariadb-10.2.11-winx64.zip

mariadb-10.2.11-win32.zip

mariadb-10.2.11-win32.msi   //這幾個就不用說了吧,Windows上面用的。

 

//下面這個包是包含glibc的二進位制包

mariadb-10.2.11-linux-glibc_214-x86_64.tar.gz (requires GLIBC_2.14+) 

//各linux發行版二進位制通用包,比如centos6

mariadb-10.2.11-linux-x86_64.tar.gz

//支援systemd的二進位制包 (本例就是用這個) 比如centos7 systemd

mariadb-10.2.11-linux-systemd-x86_64.tar.gz (for systems with systemd)

 

//下面這幾個是32位linux的包

mariadb-10.2.11-linux-i686.tar.gz

mariadb-10.2.11-linux-systemd-i686.tar.gz (for systems with systemd)

mariadb-10.2.11-linux-glibc_214-i686.tar.gz (requires GLIBC_2.14+)

 

 

//下面這兩個是rpm包

Debian and Ubuntu Packages

Red Hat, Fedora, and CentOS Packages

Mariadb安裝步驟

#建立mysql使用者和使用者組,並禁止使用者登陸

[[email protected] local]# groupadd mysql

[[email protected] local]# useradd -r -g mysql -s /sbin/nologin mysql

 

#下載mariadb包:

wget https://downloads.mariadb.org/interstitial/mariadb-10.2.11/bintar-linux-systemd-x86_64/mariadb-10.2.11-linux-systemd-x86_64.tar.gz

 

#解壓:

tar xf mariadb-10.2.11-linux-systemd-x86_64.tar.gz

 

#更改安裝目錄:注意不要提前建立/usr/local/mysql目錄,直接放過去即可

mv mariadb-10.2.11-linux-systemd-x86_64 /usr/local/mysql/

 

#進入/usr/local/mysql目錄

[[email protected] mysql]#cd  /usr/local/mysql

[[email protected] mysql]# ll

total 204

drwxr-xr-x.  2 1021 1004  4096 Oct 26 14:04 bin

-rw-r--r--.  1 1021 1004 17987 Nov 27 18:09 COPYING

-rw-r--r--.  1 1021 1004 86263 Nov 27 18:09 COPYING.thirdparty

-rw-r--r--.  1 1021 1004  2275 Nov 27 18:09 CREDITS

drwxrwxr-x.  3 1021 1004  4096 Nov 28 06:16 data

drwxr-xr-x.  2 root root  4096 Dec  4 23:06 etc

-rw-r--r--.  1 1021 1004  8245 Nov 27 18:09 EXCEPTIONS-CLIENT

drwxrwxr-x.  3 1021 1004  4096 Nov 28 06:15 include

-rw-r--r--.  1 1021 1004  8694 Nov 27 18:09 INSTALL-BINARY

drwxr-xr-x.  4 1021 1004  4096 Nov 14 22:35 lib

drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:16 man

drwxrwxr-x. 11 1021 1004  4096 Nov 28 06:15 mysql-test

-rw-r--r--.  1 1021 1004  2374 Nov 27 18:09 README.md

-rw-r--r--.  1 1021 1004 19510 Nov 27 18:09 README-wsrep

drwxrwxr-x.  2 1021 1004  4096 Nov 28 06:15 scripts

drwxrwxr-x. 32 1021 1004  4096 Nov 28 06:15 share

drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:15 sql-bench

drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:15 support-files

 

 

相關目錄介紹:

bin: 可執行的二進位制程式的存放目錄,客戶端程式mysql就位於這個目錄下。

COPYING:版權以及開源資訊

COPYING.thirdparty: 版權資訊

CREDITS:關於MariaDB軟體基金會的一些相關資訊,裡面還有國內的網際網路巨頭阿里

data:預設的資料庫存放目錄,如果我們一開始沒有指定資料庫存放目錄的話,那就會被儲存到這個位置。

EXCEPTIONS-CLIENT:例外情況

include:MariaDB 所需要的一些程式檔案

INSTALL-BINARY: 安裝幫助文件,可以詳細閱讀,對安裝資料庫有很大的幫助

lib: 軟體執行所需要的庫檔案

man:軟體的幫助文件

mysql-test: 資料庫的測試元件

scipts:mysql初始化初始化時要用到的指令碼檔案,通讀一下指令碼,可以瞭解Mysql 的安裝過程

share: 共享的檔案內容

support-files: mysql 正常執行所需要的配置檔案或者文件,這一點很重要,如果我們要自定義配置檔案的話,就需要參考這裡面的配置檔案來進行定義。

 

這裡有一點需要注意:data目錄是資料庫的存放路徑,我們在之前已經手動指定。在實際生產中,企業資料增長很快,資料庫檔案有可能會很大,因此最好將該目錄指定到一個單獨的磁碟上,或者大分割槽,或者使用邏輯卷都可以,避免因物理空間不足,導致出現故障。

 

#建立mysql配置檔案目錄

mkdir  /usr/local/mysql/etc

 

#在/usr/local/mysql/support-files/下的配置檔案模板,已經配置好的部分引數,分別用於不同的環境,這裡說明一下:

my-small.cnf 這個是為小型資料庫或者個人測試使用的,不能用於生產環境

my-medium.cnf 這個適用於中等規模的資料庫,比如個人專案或者小型企業專案中,

my-large.cnf 一般用於專門提供SQL服務的伺服器中,即專門執行資料庫服務的主機,配置要求要更高一些,適用於生產環境

my-huge.cnf 用於企業級伺服器中的資料庫服務,一般更多用於生產環境使用

所以根據以上幾個檔案,如果個人使用或者測試,那麼可以使用前兩個模板;企業伺服器或者64G以上的高配置伺服器可以使用後面兩個模板,另外也可以根據自己的需求來加大引數和擴充配置獲得更好的效能。

 

[[email protected] mysql]# ll support-files/

-rw-r--r--. 1 1021 1004  4914 Nov 27 18:32 my-huge.cnf

-rw-r--r--. 1 1021 1004 20421 Nov 27 18:32 my-innodb-heavy-4G.cnf

-rw-r--r--. 1 1021 1004  4901 Nov 27 18:32 my-large.cnf

-rw-r--r--. 1 1021 1004  4914 Nov 27 18:32 my-medium.cnf

-rw-r--r--. 1 1021 1004  2840 Nov 27 18:32 my-small.cnf

 

#複製my-medium.cnf到etc下並改名為my.cnf,修改資料庫檔案存放的目錄

cp support-files/my-medium.cnf  etc/my.cnf

vi etc/my.cnf

 

# The MariaDB server

[mysqld]

port            = 3306  

socket          = /tmp/mysql.sock

basedir = /usr/local/mysql   //mysql目錄

datadir= /usr/local/mysql/data  //資料存放位置

innodb_file_per_table = on  //每張表一個單獨檔案,便於管理

skip_name_resolve = on  //忽略反向解析,加快訪問速度

skip-external-locking

key_buffer_size = 16M

max_allowed_packet = 1M

table_open_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

 

:wq儲存後給許可權

[[email protected] local]# chown -R mysql.mysql /usr/local/mysql/

[[email protected] local]# chmod -R 755 /usr/local/mysql/

 

#新增MySQL到PATH環境變數裡面去,省的以後敲命令麻煩

[[email protected] mysql]# vi /etc/profile.d/mysql.sh

export  PATH=/usr/local/mysql/bin:$PATH

 

source /etc/profile.d/mysql.sh  使這個配置檔案及時生效

 

#初始化mysql

/usr/local/mysql/scripts/mysql_install_db --user=mysql

 

#建立mariadb自啟動指令碼,並加入開機啟動

cp /usr/local/mysql/support-files/systemd/mariadb.service  /usr/lib/systemd/system/

 

systemctl enable mariadb

systemctl start mariadb

systemctl stop mariadb

MySQL的安全設定

[[email protected] mysql]# ./bin/mysql_secure_installation

 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

 

In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

 

Enter current password for root (enter for none):   //直接回車,預設為空

OK, successfully used password, moving on...

 

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

 

Set root password? [Y/n] y     //是否設定MySQL管理員root的密碼,y設定,輸入2次

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

... Success!

 

 

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

 

Remove anonymous users? [Y/n] y    //是否刪除匿名賬戶 y刪除

... Success!

Normally, root should only ba

 

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

 

Remove test database and access to it? [Y/n] y   //是否刪除test測試資料庫,y刪除

- Dropping test database...

... Success!

- Removing privileges on test database...

... Success!

 

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

 

Reload privilege tables now? [Y/n] y   //重新載入可用的資料庫表  y 是

... Success!

 

Cleaning up...

 

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

 

Thanks for using MariaDB!