最近作項目,以前我是用 Oracle 的,若是在 Windows 環境,不管是安裝,仍是配置,都很容易;Linux 環境沒試過,剛畢業時的那個公司,是 Linux 環境。通常等到開發的時候,數據庫早按完了,這活輪不到開發幹。如今,跳槽後,公司用 MySQL~遇到兩個問題:html
- 1,對一個包含二進制字段的表,開發環境和測試環境的性能差別巨大,都是虛擬機。開發環境,Windows 平臺,執行 INSERT 很快,幾毫秒的事;而測試環境,Linux 平臺,執行 INSERT 慢到有點說不過去了,幾十毫秒,差距將近 30 倍。若是是 Oracle,前期的話,即使什麼不作,也很快;
網上有些人告訴我,應該查看一下機器的負載,好比,磁盤、IO 等,我卻是想,也不是產品環境,不至於差別這麼大吧,而且一直就沒快過~mysql
- 2,產品環境,70W 數據,執行 SELECT * FROM T1 WHERE FLAG=0 ORDER BY ID ASC,若是不爲 FLAG 字段建索引,執行時間都快 1 秒啦。這要是在 Oracle 上,絕對不可能發生~
這促使我本身在 Linux 上安裝 MySQL 源代碼。沒事時,練習、研究一下~sql
用源代碼方式安裝的好處是,編譯時,能夠針對本身的硬件環境。只是安裝相比 RPM 要麻煩點,不過「會者不難,難者不會」,多練練就行了~數據庫
期間,最開始用的是 RPM 包,挺容易,嘗試了 Percona 和社區版,最後,又用源代碼安裝了一遍~安全
本文是以 MySQL 源代碼方式進行安裝。參考衆多資料,折騰好幾天,總算安上了~若是安裝時,缺乏必要的包,就用 yum 安裝,好比 cmake、perl 等~服務器
這步的目的,是執行 mysql_install_db 建立 MySQL 受權表,以及啓動 MySQL 時,都須要指定用戶名。因此,它是第一步。app
[root@vcyber usr]# groupadd mysql
[root@vcyber usr]# useradd -g mysql mysql
假設你把 MySQL 源代碼包放在 /usr/local/src 目錄下。socket
[root@vcyber /]# cd /usr/local/src
[root@vcyber src]# ls
mysql-5.6.28.tar.gz
[root@vcyber src]# tar zxf mysql-5.6.28.tar.gz
[root@vcyber src]# ls
mysql-5.6.28 mysql-5.6.28.tar.gz
[root@vcyber src]#
此時,會看見一個新目錄 mysql-5.6.28。tcp
其中,解壓時,-x 爲解壓;-z 爲包有gzip屬性;-f 爲使用檔案名字。固然,也能夠用 -v 顯示解壓過程。函數
如下兩步任選其一,MySQL 早期版本,提供 Configure 文件以便在編譯安裝前進行配置;但在高版本已不提供該文件,而是採用 cmake。
假設,MySQL 安裝到 /usr/local/mysql 下。
[root@vcyber mysql-5.6.28]# ./configure \
> --prefix=/usr/local/mysql \
> --with-extra-charsets=all
若是採用 cmake 就跳過這步。
[root@vcyber mysql-5.6.28]# cmake \
> -DDEFAULT_CHARSET=utf8
> -DDEFAULT_COLLATION=utf8_general_ci
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
> -DEXTRA_CHARSETS=all
指定 MySQL 的安裝位置爲「/usr/local/mysql」,以及採用的默認字符集等。
我在試驗時,若是不配置字符集的相關選項,在執行後面 mysql_install_db 那步時會報各類字符集錯誤。
cmake 具體參數,參看 MySQL Source-Configuration Options
編譯和安裝很簡單。
[root@vcyber mysql-5.6.28]# make && make install
[root@vcyber mysql-5.6.28]# make
[root@vcyber mysql-5.6.28]# make install
自此,MySQL 就會安裝到 /usr/local/mysql 下。
[root@vcyber /]# cd /usr/local/src/mysql-5.6.28
[root@vcyber mysql-5.6.28]# cp support-files/my-default.cnf /etc/my.cnf
並用 vi /etc/my.cnf 修改該配置文件,內容以下。
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
sock = /tmp/mysql.sock
模板文件不僅是源代碼中有,安裝目錄中也有。
當 MySQL 發生故障或須要新加一個 mysql 實例時,須要初始化 mysql 數據庫。使用 --help 能夠查看支持的選項。
[root@vcyber mysql]# cd /usr/local/mysql
root@vcyber mysql]# scripts/mysql_install_db --user=mysql
Installing MySQL system tables...2016-02-02 02:42:33 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-02-02 02:42:33 0 [Note] ./bin/mysqld (mysqld 5.6.28) starting as process 28034 ...
2016-02-02 02:42:33 28034 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-02-02 02:42:33 28034 [Note] InnoDB: The InnoDB memory heap is disabled
2016-02-02 02:42:33 28034 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-02-02 02:42:33 28034 [Note] InnoDB: Memory barrier is not used
2016-02-02 02:42:33 28034 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-02-02 02:42:33 28034 [Note] InnoDB: Using Linux native AIO
2016-02-02 02:42:33 28034 [Note] InnoDB: Using CPU crc32 instructions
2016-02-02 02:42:34 28034 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-02-02 02:42:34 28034 [Note] InnoDB: Completed initialization of buffer pool
2016-02-02 02:42:34 28034 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
……
若是用 root 用戶運行上面的命令,應該使用 --user 選項,選項的值應與你在第一步爲運行服務器所建立的登陸帳戶(本例是 mysql 用戶)相同。若是用 mysql 用戶登陸,運行上面命令,能夠忽略 --user 選項。
用 mysql_install_db 建立 MySQL 受權表後,須要手動從新啓動服務器。
若是執行這步時報錯,說明你的第三步有問題。
[root@vcyber mysql-5.6.28]# cd /usr/local/mysql
[root@vcyber mysql]# pwd
/usr/local/mysql
[root@vcyber mysql]# chown -R root .
[root@vcyber mysql]# chown -R mysql data
[root@vcyber mysql]# chgrp -R mysql .
[root@vcyber mysql]# ls -l
total 180
drwxr-xr-x 2 root mysql 4096 Feb 2 02:23 bin
-rw-r--r-- 1 root mysql 17987 Nov 16 17:38 COPYING
drwxr-xr-x 5 mysql mysql 4096 Feb 2 09:20 data
drwxr-xr-x 2 root mysql 4096 Feb 2 02:23 docs
drwxr-xr-x 3 root mysql 4096 Feb 2 02:23 include
-rw-r--r-- 1 root mysql 105684 Nov 16 18:45 INSTALL-BINARY
drwxr-xr-x 3 root mysql 4096 Feb 2 02:23 lib
drwxr-xr-x 4 root mysql 4096 Feb 1 08:45 man
-rw-r--r-- 1 root mysql 943 Feb 1 08:46 my.cnf
-rw-r--r-- 1 root mysql 943 Feb 2 02:42 my-new.cnf
drwxr-xr-x 10 root mysql 4096 Feb 1 08:45 mysql-test
-rw-r--r-- 1 root mysql 2496 Nov 16 17:38 README
drwxr-xr-x 2 root mysql 4096 Feb 1 08:45 scripts
drwxr-xr-x 28 root mysql 4096 Feb 1 08:45 share
drwxr-xr-x 4 root mysql 4096 Feb 1 08:45 sql-bench
drwxr-xr-x 2 root mysql 4096 Feb 1 08:45 support-files
[root@vcyber mysql]#
注意第三列的變化。另外,
- 「chown –R root .」,將文件的全部屬性改成 root 用戶。注意那個點,表示全部文件;
- 「chown –R mysql data」,將數據目錄的全部屬性改成 mysql 用戶;
- 「chgrp –R mysql .」,將組屬性改成 mysql 組。注意那個點,表示全部文件。
到目前爲止,全部須要的東西都安裝完成,能夠啓動 MySQL 服務了。
[root@vcyber ~]# cd /usr/local/mysql
[root@vcyber mysql]# bin/mysqld_safe --user=mysql
[root@vcyber mysql]#
下面,驗證一下,MySQL 服務是否正常。
[root@vcyber mysql]# netstat -tnl | grep 3306
tcp 0 0 :::3306 :::* LIST EN
[root@vcyber mysql]#
[root@vcyber mysql]# pwd
/usr/local/mysql
[root@vcyber mysql]# bin/mysqladmin version
bin/mysqladmin Ver 8.42 Distrib 5.6.28, for Linux on x86_64
Copyright (c) 2000, 2015, 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.
Server version 5.6.28
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /tmp/mysql.sock
Uptime: 9 min 7 sec
Threads: 1 Questions: 9 Slow queries: 0 Opens: 67 Flush tables: 1 Open tables: 60 Queries per second avg: 0.016
[root@vcyber mysql]#
[root@vcyber mysql]# bin/mysqladmin variables
+--------------------------------------------------------+----------------------------------------------------------------------------------+
| Variable_name | Value |
+--------------------------------------------------------+----------------------------------------------------------------------------------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
| autocommit | ON |
| automatic_sp_privileges | ON |
| avoid_temporal_upgrade | OFF |
| back_log | 80 |
| basedir | /usr/local/mysql |
…… |
+--------------------------------------------------------+----------------------------------------------------------------------------------+
[root@vcyber mysql]#
使用 mysql_install_db 安裝 MySQL 數據庫受權表,定義了初始 MySQL 用戶帳號和訪問權限,全部帳號均沒有密碼。這些帳號爲超級用戶,能夠執行任何操做。
初始 root 帳戶的密碼爲空,任何人能夠用 root 帳戶不用輸入任何密碼就能夠鏈接 MySQL 服務器,並具備全部權限,這意味着 MySQL 安裝未受保護。因此,應該爲匿名帳戶指定密碼或刪除匿名帳戶,爲 MySQL root 帳戶指定密碼。使用「mysql –u root」啓動 MySQL 客戶端控制檯,鏈接 MySQL 服務器。命令行以下。
[root@vcyber mysql]# cd /usr/local/mysql
[root@vcyber mysql]# bin/mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.28 Source distribution
Copyright (c) 2000, 2015, 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> select now();
+---------------------+
| now() |
+---------------------+
| 2016-02-02 08:02:31 |
+---------------------+
1 row in set (0.00 sec)
mysql>
mysql> select * from mysql.user \G
*************************** 1. row ***************************
Host: localhost
User: root
Password:
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string:
password_expired: N
*************************** 2. row ***************************
Host: vcyber
User: root
Password:
Select_priv: Y
……: ……
*************************** 3. row ***************************
Host: 127.0.0.1
User: root
Password:
Select_priv: Y
……: ……
*************************** 4. row ***************************
Host: ::1
User: root
Password:
Select_priv: Y
……: ……
*************************** 5. row ***************************
Host: localhost
User:
Password:
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: NULL
password_expired: N
*************************** 6. row ***************************
Host: vcyber
User:
Password:
Select_priv: N
……: ……
6 rows in set (0.00 sec)
mysql>
mysql> DELETE FROM mysql.user WHERE Host='localhost' and User='';
Query OK, 1 rows affected(0.08 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 1 rows affected(0.08 sec)
mysql> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('123456');
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> exit
[root@vcyber mysql]#
[root@vcyber mysql]# bin/mysql -u root –h localhost -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.28 Source distribution
Copyright (c) 2000, 2015, 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 服務器的 mysqladmin 命令,經過 –u 選項給出數據庫管理員用戶 root,-p 選項給出密碼,便可關閉 MySQL 服務器。
[root@vcyber mysql]# bin/mysqladmin -u root -p shutdown
Enter password:
[root@vcyber mysql]#
[root@vcyber mysql]# cd /usr/local/src/mysql-5.6.28
[root@vcyber mysql-5.6.28]# pwd
/usr/local/src/mysql-5.6.28
[root@vcyber mysql-5.6.28]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@vcyber mysql-5.6.28]#
此時,就已經能夠手動啓動/中止 MySQL 數據庫。命令以下:
[root@vcyber mysql]# service mysqld status
SUCCESS! MySQL running (24426)
[root@vcyber mysql]# service mysqld stop
Shutting down MySQL..141223 11:37:29 mysqld_safe mysqld from pid file /mysql/data/testdb1.pid ended
SUCCESS!
[1]+ Done bin/mysqld_safe --user=mysql (wd: /mysql)
(wd now: /etc)
[root@vcyber mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@vcyber mysql]#
但若想讓 MySQL 在開機時自動啓動,還須要以下設置。
修改 /etc/rc.d/init.d/mysqld 權限,以下所示:
[root@vcyber mysql-5.6.28]# chown root.root /etc/rc.d/init.d/mysqld
[root@vcyber mysql-5.6.28]# chmod 755 /etc/rc.d/init.d/mysqld
[root@vcyber mysql-5.6.28]#
使用 chkconfig 命令設置在不一樣系統運行級別下的自啓動策略,首先使用「chkconfig --add mysqld」命令增長所指定的 mysqld 服務,讓 chkconfig 指令得以管理它,並同時在系統啓動的敘述文件內增長相關數據。使用命令以下所示:
[root@vcyber mysql-5.6.28]# chkconfig --add mysqld
[root@vcyber mysql-5.6.28]#
而後,使用「chkconfig --level 3 mysqld on」命令和「chkconfig --level 5 mysqld on」命令,在第三等級和第五等級中開啓 mysqld 服務,即在字符模式和圖形模式啓動時自動開啓 mysqld 服務。命令以下所示:
[root@vcyber mysql-5.6.28]# chkconfig --level 3 mysqld on
[root@vcyber mysql-5.6.28]# chkconfig --level 5 mysqld on
[root@vcyber mysql-5.6.28]#
再使用「chkconfig --list」命令檢查設置。命令以下所示:
[root@vcyber mysql-5.6.28]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@vcyber mysql-5.6.28]#
自此,MySQL 安裝配置結束。
設置環境變量後,你就能夠不用輸入 MySQL 客戶端命令的全路徑啦。
編輯 etc/my.cnf,追加以下內容:
[mysqld]
lower_case_table_names=1
設置環境:
[root@vcyber mysql]# cd /etc
[root@vcyber etc]# vi profile
在文件最後增長:
PATH=/opt/mysql/bin:/opt/mysql/lib:$PATH
export PATH
生效環境變量:
[root@vcyber etc]# source profile