1. 程式人生 > >MySQL資料庫多種安裝方法及企業級安裝實踐(下篇)

MySQL資料庫多種安裝方法及企業級安裝實踐(下篇)

接《MySQL資料庫多種安裝方法及企業級安裝實踐(上篇)》

3.2.3初始化MySQL資料庫檔案

上述配置完畢後,就可以初始化資料庫檔案了,這個步驟其實也可以在編譯安裝MySQL之後就操作,只不過放到這裡更合理一些。

(1)初始化MySQL資料庫

初始化資料庫的核心命令為:

application/mysql/scripts/mysql_install_db –basedir=/application/mysql/ –datadir=/application/mysql/data –user=mysql

提示:–basedir=/application/mysql/為MySQL的安裝路徑,–datadir為資料檔案目錄。另,注意mysql_install_db和MySQL5.1的路徑不同,MySQL5.1不在MySQL bin路徑下了。

整個初始化的操作過程為:

[[email protected] mysql-5.6.34]# /application/mysql/scripts/mysql_install_db –basedir=/application/mysql/ –datadir=/application/mysql/data –user=mysql

#<==初始化mysql資料庫檔案,會有很多資訊提示,如果沒有ERROR級別的錯誤,有兩個OK的字樣,表示初始化成功,否則就要解決初始化的問題。

Installing MySQL system tables…2017-02-26 18:08:53 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).

2017-02-26 18:08:53 0 [Note] Ignoring –secure-file-priv value as server is running with –bootstrap.

2017-02-26 18:08:53 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.34) starting as process 47993 …

2017-02-26 18:08:53 47993 [Note] InnoDB: Using atomics to ref count buffer pool pages

2017-02-26 18:08:53 47993 [Note] InnoDB: The InnoDB memory heap is disabled

2017-02-26 18:08:53 47993 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2017-02-26 18:08:53 47993 [Note] InnoDB: Memory barrier is not used

2017-02-26 18:08:53 47993 [Note] InnoDB: Compressed tables use zlib 1.2.3

2017-02-26 18:08:53 47993 [Note] InnoDB: Using Linux native AIO

2017-02-26 18:08:53 47993 [Note] InnoDB: Using CPU crc32 instructions

2017-02-26 18:08:53 47993 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2017-02-26 18:08:53 47993 [Note] InnoDB: Completed initialization of buffer pool

2017-02-26 18:08:53 47993 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!

2017-02-26 18:08:53 47993 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB

2017-02-26 18:08:53 47993 [Note] InnoDB: Database physically writes the file full: wait…

…省略若干…

2017-02-26 18:08:54 47993 [Note] InnoDB: Waiting for purge to start

2017-02-26 18:08:54 47993 [Note] InnoDB: 5.6.34 started; log sequence number 0

2017-02-26 18:08:54 47993 [Note] Binlog end

2017-02-26 18:08:54 47993 [Note] InnoDB: FTS optimize thread exiting.

2017-02-26 18:08:54 47993 [Note] InnoDB: Starting shutdown…

2017-02-26 18:08:55 47993 [Note] InnoDB: Shutdown completed; log sequence number 1625977

OK 

#<==兩個OK是初始化成功的標誌。

Filling help tables…2017-02-26 18:08:55 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).

2017-02-26 18:08:55 0 [Note] Ignoring –secure-file-priv value as server is running with –bootstrap.

2017-02-26 18:08:55 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.34) starting as process 48015 …

2017-02-26 18:08:55 48015 [Note] InnoDB: Using atomics to ref count buffer pool pages

2017-02-26 18:08:55 48015 [Note] InnoDB: The InnoDB memory heap is disabled

…省略若干…

2017-02-26 18:08:56 48015 [Note] InnoDB: FTS optimize thread exiting.

2017-02-26 18:08:56 48015 [Note] InnoDB: Starting shutdown…

2017-02-26 18:08:57 48015 [Note] InnoDB: Shutdown completed; log sequence number 1625987

OK 

#<==兩個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 MySQL root USER !

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

/application/mysql//bin/mysqladmin -u root password ‘new-password’

/application/mysql//bin/mysqladmin -u root -h oldboy password ‘new-password’

Alternatively you can run:

/application/mysql//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 manual for more instructions.

You can start the MySQL daemon with:

cd . ; /application/mysql//bin/mysqld_safe &

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

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

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

###請注意如下幾行英文的說明

New default config file was created as /application/mysql//my.cnf and

will be used by default by the server when you start it.

You may edit this file to change server settings

#從上文說明中可以指導mysql的預設配置檔案已經變到了/application/mysql//my.cnf

WARNING: Default config file /etc/my.cnf exists on the system

This file will be read by default by the MySQL server

If you do not want to use this, either remove it, or use the

–defaults-file argument to mysqld_safe when starting the server

#從上文說明中可以看到資料庫啟動時會讀取/etc/my.cnf,因此有可能會導致無法啟動,避免的方法就是使用mysqld_safe 啟動服務時採用–defaults-file引數指定配置檔案,前文已將/etc/my.cnf改名了,所以,就不需要指定引數了,這裡是一個坑,讀者要注意。

此步驟必須要初始化成功,否則,後面會出現登入不了資料庫等各種問題。

(2)初始化資料庫的原理及結果說明

初始化資料庫的實質就是建立基礎的資料庫系統的庫檔案,例如:生成MySQL庫表等。

初始化資料庫後,檢視資料目錄,可以看到多瞭如下檔案:

[[email protected] mysql-5.6.34]# ls -l /application/mysql/data

total 110604

-rw-rw—-. 1 mysql mysql 12582912 Feb 26 18:08 ibdata1

-rw-rw—-. 1 mysql mysql 50331648 Feb 26 18:08 ib_logfile0

-rw-rw—-. 1 mysql mysql 50331648 Feb 26 18:08 ib_logfile1

drwx——. 2 mysql mysql     4096 Feb 26 18:08 mysql   #<==用於存放管理mysql的資料。

drwx——. 2 mysql mysql     4096 Feb 26 18:08 performance_schema  #<==5.5及以上增加的內部效能庫。

drwxr-xr-x. 2 mysql mysql     4096 Feb 26 17:48 test    #<==用於測試的test資料庫。

[[email protected] mysql-5.6.34]# tree /application/mysql/data #<==如果沒有tree,可以yum install tree –y安裝

/application/mysql/data

├── ibdata1

├── ib_logfile0

├── ib_logfile1

├── mysql

│   ├── columns_priv.frm

│   ├── columns_priv.MYD

│   ├── columns_priv.MYI

…省略若干…

│   ├── general_log.CSV

│   ├── general_log.frm

│   ├── help_category.frm

│   ├── procs_priv.frm

│   ├── procs_priv.MYD

│   ├── procs_priv.MYI

│   ├── proxies_priv.frm

│   ├── proxies_priv.MYD

│   ├── proxies_priv.MYI

│   ├── servers.frm

…省略若干…

│   ├── time_zone_name.MYI

│   ├── time_zone_transition.frm

│   ├── time_zone_transition.MYD

│   ├── time_zone_transition.MYI

│   ├── time_zone_transition_type.frm

│   ├── time_zone_transition_type.MYD

│   ├── time_zone_transition_type.MYI

│   ├── user.frm

│   ├── user.MYD

│   └── user.MYI

├── performance_schema

│   ├── accounts.frm

│   ├── cond_instances.frm

│   ├── db.opt

│   ├── events_stages_current.frm

│   ├── events_stages_history.frm

│   ├── events_stages_history_long.frm

│   ├── events_stages_summary_by_account_by_event_name.frm

…省略若干…

│   ├── threads.frm

│   └── users.frm

└── test

    └── db.opt

3 directories, 136 files

(3)MySQL初始化故障排錯集錦

本節的所有故障必須要解除,否則,後面會出現登入不了MySQL資料庫等各種問題,故障集錦見本章結尾內容。

3.2.4配置並啟動MySQL資料庫

1)設定MySQL啟動指令碼

[[email protected] mysql-5.6.34]# pwd

/home/oldboy/tools/mysql-5.6.34

[[email protected] mysql-5.6.34]# cp support-files/mysql.server /etc/init.d/mysqld #<==拷貝mysql啟動指令碼到mysql的命令路徑。

[[email protected] mysql-5.6.34]# chmod 700 /etc/init.d/mysqld  #<==使指令碼可執行。

[[email protected] mysql-5.6.34]# ls -l /etc/init.d/mysqld

-rwx——. 1 root root 10929 Feb 26 18:26 /etc/init.d/mysqld

2)啟動MySQL資料庫

#<==這是啟動資料庫規範方法之一,還可以使用/application/mysql/bin/mysqld_safe –user=mysql &啟動。這個命令結尾 “&”符號作用是,在後臺執行mysql服務,這條命令執行完 還需要按下回車才能進入到命令列狀態。

#<==注意,如果已執行上面/etc/init.d/mysqld start啟動命令,還想嘗試下面mysqld_safe的命令,請先執行/etc/init.d/mysqld stop結束mysql程序。

[[email protected] mysql-5.6.34]# cd ~

[[email protected] mysql-5.6.34]# /etc/init.d/mysqld start

Starting MySQL. SUCCESS!

提示:禁止使用pkill、kill -9、killall -9等命令強制殺死資料庫,這會引起資料庫無法啟動等故障發生。企業中曾發生過的血的教訓案例請看http://oldboy.blog.51cto.com/2561410/1431161。

3)檢查MySQL資料庫是否啟動。

[[email protected] ~]# netstat -lntup|grep mysql 

tcp        0      0 :::3306              :::*                  LISTEN      48065/mysqld

如發現3306埠沒起來。請使用tail -100 /application/mysql/data/機器名.err檢查日誌報錯進行除錯。經常檢視服務執行日誌是個很好的習慣,也是高手的習慣,你要不要成為高手?

4)檢視MySQL資料庫啟動結果日誌。

[[email protected] ~]# tail /application/mysql/data/oldboy.err

2017-02-26 17:38:36 48065 [Note] InnoDB: Waiting for purge to start

2017-02-26 17:38:36 48065 [Note] InnoDB: 5.6.34 started; log sequence number 1625987

2017-02-26 17:38:36 48065 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4f94404a-fc74-11e6-8112-000c292ece3f.

2017-02-26 17:38:36 48065 [Note] Server hostname (bind-address): ‘*’; port: 3306

2017-02-26 17:38:36 48065 [Note] IPv6 is available.

2017-02-26 17:38:36 48065 [Note]   – ‘::’ resolves to ‘::’;

2017-02-26 17:38:36 48065 [Note] Server socket created on IP: ‘::’.

2017-02-26 17:38:36 48065 [Note] Event Scheduler: Loaded 0 events

2017-02-26 17:38:36 48065 [Note] /application/mysql-5.6.34/bin/mysqld: ready for connections.

Version: ‘5.6.34’  socket: ‘/application/mysql-5.6.34/tmp/mysql.sock’  port: 3306  Source distribution

本例查看了錯誤日誌的命令及錯誤日誌中的內容,這裡省略了大部分日誌內容,只給了預設10行,如果有錯誤,一般會顯示error字樣。

5)設定MySQL開機自啟動。

[[email protected] ~]# chkconfig –add mysqld

[[email protected] ~]# chkconfig –list mysqld

mysqld          0:off     1:off    2:on 3:on 4:on 5:on 6:off此外,將啟動命令/etc/init.d/mysqld start放到/etc/rc.local裡面實現開機自啟動也可。

若MySQL安裝及使用出現故障,可根據下面的分析思路進行檢查。

細看所有執行命令返回的螢幕輸出,不要忽略關鍵的輸出內容。

輔助檢視系統日誌/var/log/messages。

如果是MySQL關聯了其他服務,要同時檢視相關服務的日誌。

仔細閱讀,重新檢視操作的步驟是否正確,書寫的命令及字元是不是都對。

經常檢視各種服務執行日誌是個很好的習慣,也是成長為高手的必經之路,你要不要成為高手?

3.2.5 將MySQL相關命令加入全域性路徑

如果不為MySQL的命令配置全域性路徑,就無法直接在命令列輸入mysql這樣的命令,只能用全路徑命令(/application/mysql/bin/mysql),這種帶著路徑輸入命令的方式很麻煩。下面來看看配置的具體方法。

1)確認mysql命令所在的路徑。

[email protected] /]# ls /application/mysql/bin/mysql

/application/mysql/bin/mysql

2)在PATH變數前面增加/application/mysql/bin路徑,並追加到/etc/profile檔案中。

[[email protected] /]# echo ‘export PATH=/application/mysql/bin:$PATH’ >>/etc/profile

#<==注意,echo後是單引號呦,雙引號是不行滴。

[[email protected] /]# tail -1 /etc/profile

export PATH=/application/mysql/bin:$PATH

[[email protected] /]# source /etc/profile

#<==執行source使上一行新增到/etc/profile中,內容直接生效

#<==以上命令的用途為,定義mysql全域性路徑,實現在任意路徑執行mysql命令。

[[email protected] ~]# echo $PATH

/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

#<==執行echo $PATH有/application/mysql/bin輸出表示配置成功。

提示:更簡單的設定方法為用下面命令做軟連結:ln -s /application/mysql/bin/* /usr/local/sbin/,把mysql命令所在路徑連結到全域性路徑/usr/local/sbin/的下面。

要特別強調的是,務必把MySQL命令路徑放在PATH路徑中其他路徑的前面,否則,可能會導致使用的mysql等命令和編譯安裝的mysql命令不是同一個,進而產生錯誤。下面的網址中給出了因為MySQL路徑配置問題導致的錯誤案例:http://oldboy.blog.51cto.com/2561410/1122867,該錯誤實際上就是因為使用yum安裝的MySQL客戶端命令訪問了編譯安裝的服務端而導致的。

3.2.6登入MySQL測試

登入並測試的命令如下:

[[email protected] ~]# mysql #<==直接敲mysql就進入資料庫了,而且身份還是root。

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.6.34 Source distribution

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>

提示:你還可以使用如下三種命令寫法登入mysql:

mysql -uroot -p,

mysql -uroot,

mysql -uroot -p ‘密碼’

若出現登入故障,可通過以下方法排查。

如果這裡提示無法登入,排除了資料庫啟動問題後,則很可能是資料庫初始化檔案有問題。例如:

[[email protected] ~]# mysql

ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

解決辦法:重新初始化資料庫即可,此問題一般都是資料庫初始化問題,或者資料庫檔案損壞,以及目錄許可權問題。

mysql> show databases;  #<==檢視當前的資料庫

+——————–+

| Database           |

+——————–+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+——————–+

4 rows in set (0.00 sec)

mysql> select user();  #<==檢視當前的登入使用者

+—————-+

| user()         |

+—————-+

| [email protected] |

+—————-+

1 row in set (0.00 sec)

mysql>   #<==快捷鍵ctrl+d,也可以使用quit或exit等。

MySQL安裝完成後,預設情況下,管理員賬號root是無密碼的,極不安全,必須要處理一下。

3.2.7基本的MySQL安全配置

1. 為root使用者設定密碼

MySQL管理員的賬號root密碼預設為空,極不安全,可以通過mysqladmin命令為mysql不同例項的資料庫設定獨立的密碼。

[[email protected] ~]# mysqladmin -u root password ‘oldboy123’ #<==為root使用者設定密碼oldboy123。

Warning: Using a password on the command line interface can be insecure. #<==這裡是一個警告,提醒使用者命令列放置密碼是不安全的,讀者儘量不要在命令列輸入密碼,而是採取互動式的輸入密碼。

[[email protected] ~]# mysql #<==無法直接輸入命令登入了。

ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

[[email protected] ~]# mysql -uroot -p #<==新的登入方式,也可以直接帶密碼mysql -uroot -poldboy123。

Enter password:        #<==輸入新密碼oldboy123,互動式輸入密碼不會記錄在命令記錄裡,因此更安全。

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.6.34 Source distribution

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>

讀者也可以執行mysql_secure_installation命令互動式地設定系統的使用者密碼。

2. 清理mysql伺服器內無用的使用者

select user,host from mysql.user;

+——+———–+

| user | host      |

+——+———–+

| root | 127.0.0.1 |

| root | ::1       |

|      | localhost |

| root | localhost |

|      | oldboy    |

| root | oldboy    |

+——+———–+

6 rows in set (0.00 sec)

mysql> drop user [email protected]’::1′;

Query OK, 0 rows affected (0.00 sec)

mysql> drop user [email protected]’oldboy’;

Query OK, 0 rows affected (0.00 sec)

mysql> drop user ”@’oldboy’;

Query OK, 0 rows affected (0.00 sec)

mysql> drop user ”@’localhost’;

Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;

+——+———–+

| user | host      |

+——+———–+

| root | 127.0.0.1 |

| root | localhost |

+——+———–+

2 rows in set (0.00 sec)

提示:對於drop命令及資料庫安全,後文會有詳細講解, 此處不執行也可以。

有時用drop命令可能無法刪除對應使用者。比如,當資料庫內的host等欄位為大寫及特殊Linux主機名時,刪除使用者就會遇到問題,例如:

mysql> drop user ‘ ‘@’MySQL’;  

ERROR 1396 (HY000): Operation DROP USER failed for ‘ ‘@’mysql’

可使用DML語句,並採用delete命令刪除來解決此問題,具體命令如下:

mysql> delete from mysql.user where user=” and host=’MySQL’;

Query OK, 1 row affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

3. 刪除mysql資料庫內無用的test庫

mysql> drop database test;

Query OK, 0 rows affected (0.00 sec)

mysql> show databases;

+——————–+

| Database           |

+——————–+

| information_schema |

| mysql              |

| performance_schema |

+——————–+

3 rows in set (0.00 sec)

有關更多的MySQL的安全措施,在後文會有更詳細的講解。

3.3  MySQL安裝FAQ

問題1:在配置mysql時遇到錯誤。

出現的錯誤如下:

checking for tgetent in -ltinfo… no
checking for termcap functions library… configure: error: No curses/termcap library found

原因:缺少ncurses安裝包。

解決方法:使用yum -y install ncurses-devel命令。

問題2:初始化MySQL資料庫時出現故障。

故障1:給出了警告資訊“WARNING: The host ‘oldboy’ could not be looked up with resolveip.”

警告是可以忽略的,如果非要解決則需修改對主機名的解析,使其和uname -n命令的結果一樣,如下:

[[email protected] ~]# grep “`uname -n`” /etc/
hosts127.0.0.1  oldboy

故障2:錯誤提示ERROR: 1004  Can’t create file ‘/tmp/#sql300e_1_0.frm’ (errno: 13)

在執行初始化資料庫命令時可能就會遇到這樣的錯誤,錯誤提示如下:

ERROR: 1004  Can’t create file ‘/tmp/#sql300e_1_0.frm’ (errno: 13)
120406 15:47:02 [ERROR] Aborting
120406 15:47:02 [Note] /application/mysql/libexec/mysqld: Shutdown complete
Installation of system tables failed! Examine the logs in
/application/mysql/data for more information.

根據提示可知,/tmp目錄不能建立檔案,所以解決辦法為給/tmp目錄增加許可權,如下:

解決辦法:還原/tmp目錄許可權,操作命令如下:

[[email protected] ~]# chmod 1777 /tmp   #<==1777是/tmp的預設許可權,除非曾經改動過此目錄許可權才會報錯。
[[email protected] ~]# ls -ld /tmp/     
drwsrwxrwt. 8 root root 4096 3月  10 18:07 /tmp/

本示例的故障必須要解除,否則,後面會出現登入不了MySQL資料庫等各種問題。

問題3:登入資料庫是提示“Access denied for user ‘root’@’localhost’”

解答:正文裡已給出答案,不再累述。

問題4:有時drop刪除MySQL使用者刪不掉。

解答:正文裡已給出答案,不再累述。

3.4 MySQL編譯常見引數選項說明

編譯引數 說明
-DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.34 設定mysql安裝目錄
-DMYSQL_DATADIR=/application/mysql-5.6.34/data 設定mysql資料檔案目錄
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.34/tmp/mysql.sock 設定mysql.sock路徑
-DDEFAULT_CHARSET=utf8 設定預設的字符集為utf8
-DDEFAULT_COLLATION=utf8_general_ci 設定預設排序規則
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii 啟用額外的字符集型別
-DENABLED_LOCAL_INFILE=ON 啟用本地資料匯入支援
-DWITH_INNOBASE_STORAGE_ENGINE=1

-DWITH_FEDERATED_STORAGE_ENGINE=1

-DWITH_BLACKHOLE_STORAGE_ENGINE=1

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1

-DWITHOUT_PARTITION_STORAGE_ENGINE=1

若想啟用某個引擎的支援,-DWITH_<ENGINE>_STORAGE_ENGINE=1

若想禁用某個引擎的支援:-DWITHOUT_<ENGINE>_STORAGE_ENGINE=0

-DWITH_FAST_MUTEXES=1
-DWITH_ZLIB=bundled 啟用libz庫支援
-DENABLED_LOCAL_INFILE=1 這個引數重複了,啟用本地資料匯入支援
-DWITH_READLINE=1 啟用readline庫支援(提供可編輯的命令列)
-DWITH_EMBEDDED_SERVER=1 編譯嵌入式伺服器支援
-DWITH_DEBUG=0 禁用debug(預設為禁用)

更多內容可以參考官方MySQL 5.6的cmake編譯引數http://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html

文章來自微信公眾號:老男孩教育