在諸多開源數據庫中,MySQL是目前應用行業,特別是互聯網行業發展最好的一個。借助靈活的架構特點和適應不同應用系統場景的Storage Engine,MySQL在很多方面已經有不次於傳統商用數據庫的表現。
本篇作為MySQL的開篇,只是從最簡單的安裝着手。所謂萬事開頭難,希望筆者的學習研究過程能夠順利。
1、說說MySQL的版本
首先,我們簡單說說MySQL的版本。在Oracle收購開源數據庫MySQL之后,對MySQL進行了重新部署。目前我們看到的MySQL系統組件和版本,就是Oracle對其進行整合之后的版本。
從發行版本上,目前MySQL最新版本是5.5,每隔一個時期就會有新的子版本發行。根據收費和服務支持情況,MySQL分為社區版Community和企業版Enterprise。社區版是提供完全免費的應用授權,並且完全公布源代碼。而企業版是收費版本,要向Oracle繳納許可授權費用。所以,目前真正意義的開源數據庫,也只是以Community作為一個代表。
在Community中,根據不同人群使用的要求分為二進制版、壓縮版和源代碼版。
ü 二進制發布版(Binary Distribution)是我們經常使用,也是最容易使用的一個版本。MySQL針對不同的平台,提供了mis、rpm等安裝文件格式;
ü 壓縮版(Zip)也可以稱為“綠色版”。提供的壓縮包中,包括了安裝后所有的目錄結構和配置文件。作為最簡單的應用,只要解壓到目錄上,並且進行一些配置連接就可以了。壓縮版最大的好處是獨立於操作系統,對原有的結構沒有影響;
ü 源代碼版(Source)是針對一些個性化操作系統或者個性化功能需求而言的。需要用戶在指定的平台操作系統上進行編譯、鏈接,形成可執行版本;
本篇就簡單介紹一下Linux環境下壓縮版配置的一些問題和解決方法。
2、基本配置
首先,我們准備好Linux環境,我們使用CentOS 5.8進行試驗安裝。第一步,我們需要將下載好的MySQL壓縮版通過FTP/SFTP上傳到Linux服務器上。
--創建目錄
[root@bspdev ~]# cd /
[root@bspdev /]# mkdir mysql
[root@bspdev /]# ls -l | grep mysql
drwxr-xr-x 2 root root 4096 Jul 1 03:56 mysql
使用FTP/SFTP軟件,將壓縮包(mysql-5.5.25-linux2.6-i686.tar.gz)上傳到指定目錄,並且解壓。
[root@bspdev /]# cd mysql
[root@bspdev mysql]# ls -l
total 175376
-rw-r--r-- 1 root root 179403866 Jul 1 03:57 mysql-5.5.25-linux2.6-i686.tar.gz
[root@bspdev mysql]#tar -vxf mysql-5.5.25-linux2.6-i686.tar.gz
mysql-5.5.25-linux2.6-i686/include/my_config.h
mysql-5.5.25-linux2.6-i686/include/keycache.h
mysql-5.5.25-linux2.6-i686/include/m_string.h
mysql-5.5.25-linux2.6-i686/include/mysql_version.h
mysql-5.5.25-linux2.6-i686/include/my_alloc.h
(篇幅原因,省略……)
[root@bspdev mysql]# ls -l
total 4
drwxr-xr-x 13 root root 4096 Jul 1 04:02 mysql-5.5.25-linux2.6-i686
打開目錄,可以發現壓縮版解壓之后,所有的配置文件和執行程序目錄。
[root@bspdev mysql-5.5.25-linux2.6-i686]# ls -l
total 76
drwxr-xr-x 2 root root 4096 Jul 1 04:01 bin
-rw-r--r-- 1 7161 wheel 17987 May 15 15:46 COPYING
drwxr-xr-x 4 root root 4096 Jul 1 04:02 data
drwxr-xr-x 2 root root 4096 Jul 1 04:01 docs
drwxr-xr-x 3 root root 4096 Jul 1 04:00 include
-rw-r--r-- 1 7161 wheel 7604 May 15 15:46 INSTALL-BINARY
drwxr-xr-x 3 root root 4096 Jul 1 04:02 lib
drwxr-xr-x 4 root root 4096 Jul 1 04:00 man
drwxr-xr-x 10 root root 4096 Jul 1 04:02 mysql-test
-rw-r--r-- 1 7161 wheel 2552 May 15 15:46 README
drwxr-xr-x 2 root root 4096 Jul 1 04:01 scripts
drwxr-xr-x 27 root root 4096 Jul 1 04:02 share
drwxr-xr-x 4 root root 4096 Jul 1 04:01 sql-bench
drwxr-xr-x 2 root root 4096 Jul 1 04:01 support-files
[root@bspdev mysql-5.5.25-linux2.6-i686]# pwd
/mysql/mysql-5.5.25-linux2.6-i686
注意,無論是Windows版還是Linux版,MySQL在運行啟動的時候,都需要一個基礎配置文件。不同的操作系統拓展名稱不一樣,大都是my.ini/my.cnf等等。
在目錄的support-files中,我們可以找到MySQL提供了一套默認模板,用於配置不同規模的MySQL數據庫。
[root@bspdev mysql-5.5.25-linux2.6-i686]# cd support-files/
[root@bspdev support-files]# ls -l
total 96
-rwxr-xr-x 1 7161 wheel 1153 May 15 16:46 binary-configure
-rwxr-xr-x 1 7161 wheel 4528 May 15 16:46 config.huge.ini
-rwxr-xr-x 1 7161 wheel 2382 May 15 16:46 config.medium.ini
-rwxr-xr-x 1 7161 wheel 1626 May 15 16:46 config.small.ini
-rw-r--r-- 1 7161 wheel 773 May 15 15:46 magic
-rw-r--r-- 1 7161 wheel 4691 May 15 16:46 my-huge.cnf
-rw-r--r-- 1 7161 wheel 19759 May 15 16:46 my-innodb-heavy-4G.cnf
-rw-r--r-- 1 7161 wheel 4665 May 15 16:46 my-large.cnf
-rw-r--r-- 1 7161 wheel 4676 May 15 16:46 my-medium.cnf
-rw-r--r-- 1 7161 wheel 2840 May 15 16:46 my-small.cnf
-rwxr-xr-x 1 7161 wheel 1061 May 15 16:46 mysqld_multi.server
-rwxr-xr-x 1 7161 wheel 839 May 15 16:46 mysql-log-rotate
-rwxr-xr-x 1 7161 wheel 10650 May 15 16:46 mysql.server
-rwxr-xr-x 1 7161 wheel 1326 May 15 16:46 ndb-config-2-node.ini
--拷貝模板到指定目錄指定名稱。
[root@bspdev support-files]# cp my-huge.cnf /etc/my.cnf
標紅的幾個文件,就是針對不同的系統規模,主要是服務器內存大小來預定義的配置文件。My-huge.cnf一般是應對服務器內存在1-2G,而且是專門作為MySQL服務器使用的場景。這點對於目前的生產系統,不是什么大問題。所以,我們使用這個版作為基礎模板,復制為/etc/my.cnf。
對MySQL配置文件的目錄,不同的操作系統有不同的默認選擇。對Windows而言,默認是選擇%WINDIR%作為優先搜索目錄,找尋文件名稱為my.ini的文件。當然,我們在mysqld的啟動參數defaults-file中,也可以指定該文件的目錄名稱。
對Linux環境下,我們就將其放在/etc/my.cnf就可以了。
3、用戶和相關配置
在Oracle配置時,我們創建了專門的OS用戶來安裝Oracle。對MySQL,我們也選擇相對應的策略。
注意:root用戶一般不會充當專門的應用系統用戶。
[root@bspdev support-files]# groupadd mysql –組group創建
[root@bspdev support-files]# useradd -g mysql mysql –用戶創建
[root@bspdev support-files]# id mysql
uid=500(mysql) gid=500(mysql) groups=500(mysql)
[root@bspdev support-files]# passwd mysql --密碼修改
Changing password for user mysql.
New UNIX password:
BAD PASSWORD: it is too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@bspdev support-files]# su – mysql
此外,我們要保證mysql能夠訪問到MySQL系統bin目錄下的程序,要修改環境變量PATH。
[mysql@bspdev ~]$ vi .bash_profile
(篇幅原因,省略部分……)
PATH=$PATH:$HOME/bin
PATH=$PATH:/mysql/mysql-5.5.25-linux2.6-i686/bin
export PATH
~
~
[mysql@bspdev ~]$ su - mysql
Password:
[mysql@bspdev ~]$ env | grep PATH
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/mysql/bin:/mysql/mysql-5.5.25-linux2.6-i686/bin
最后,將相應目錄的權限賦予mysql用戶。
[mysql@bspdev ~]$ su - root
Password:
[root@bspdev ~]# cd /
[root@bspdev /]# chown -R mysql:mysql mysql/
[root@bspdev /]# ls -l | grep mysql
drwxr-xr-x 3 mysql mysql 4096 Jul 1 04:03 mysql
[root@bspdev etc]# pwd
/etc
[root@bspdev etc]# chown mysql:mysql my.cnf
[root@bspdev etc]# ls -l | grep my.cnf
-rw-r--r-- 1 mysql mysql 4691 Jul 1 04:05 my.cnf
4、配置文件修改
下面,需要對my.cnf做出簡單的修改,使其可以作為啟動文件。我們必須添加上MySQL程序文件和數據文件的目錄,這樣才能夠讓MySQLd服務器在啟動后可以找到相應的路徑。
[root@bspdev etc]# su - mysql
[mysql@bspdev ~]$ vi /etc/my.cnf
# Example MySQL config file for very large systems.
#
# This is for a large system with memory of 1G-2G where the system runs mainly
# MySQL.
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# The MySQL server
[mysqld]
basedir=/mysql/mysql-5.5.25-linux2.6-i686
datadir=/mysql/mysql-5.5.25-linux2.6-i686/data
注意,basedir和datadir是通常需要設置的內容。如果沒有設置,MySQL會根據操作系統的不同,在默認目錄上進行尋找,引起錯誤。
5、初始化數據庫
對Linux版本的壓縮版,我們需要手工的創建數據庫。在Windows版本中,很多時候是不需要進行這個步驟的。創建的方法也比較簡單,就是運行相應的腳本程序。
--在程序目錄中。
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$ scripts/mysql_install_db
Installing MySQL system tables...
OK
Filling help tables...
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:
/mysql/mysql-5.5.25-linux2.6-i686/bin/mysqladmin -u root password 'new-password'
/mysql/mysql-5.5.25-linux2.6-i686/bin/mysqladmin -u root -h bspdev.localdomain password 'new-password'
Alternatively you can run:
/mysql/mysql-5.5.25-linux2.6-i686/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 /mysql/mysql-5.5.25-linux2.6-i686 ; /mysql/mysql-5.5.25-linux2.6-i686/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /mysql/mysql-5.5.25-linux2.6-i686/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /mysql/mysql-5.5.25-linux2.6-i686/scripts/mysqlbug script!
從輸出信息,我們可以看到MySQL提供了一些信息,作為下面工作的參考。
6、啟動/關閉測試
到此,主要的安裝步驟已經完成,我們進行初步的啟動服務器試驗。
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$ mysqld &
[1] 28398
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$120701 4:21:42 [Warning] Changed limits: max_open_files: 1024 max_connections: 151 table_cache: 431
120701 4:21:42 [Note] Plugin 'FEDERATED' is disabled.
120701 4:21:42 InnoDB: The InnoDB memory heap is disabled
120701 4:21:42 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
120701 4:21:42 InnoDB: Compressed tables use zlib 1.2.3
120701 4:21:42 InnoDB: Using Linux native AIO
120701 4:21:42 InnoDB: Initializing buffer pool, size = 128.0M
120701 4:21:42 InnoDB: Completed initialization of buffer pool
InnoDB: The first specified data file ./ibdata1 did not exist:
InnoDB: a new database to be created!
120701 4:21:42 InnoDB: Setting file ./ibdata1 size to 10 MB
InnoDB: Database physically writes the file full: wait...
120701 4:21:42 InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
InnoDB: Database physically writes the file full: wait...
120701 4:21:42 InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
InnoDB: Database physically writes the file full: wait...
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: 127 rollback segment(s) active.
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
120701 4:21:43 InnoDB: Waiting for the background threads to start
120701 4:21:44 InnoDB: 1.1.8 started; log sequence number 0
120701 4:21:44 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
120701 4:21:44 [Note] - '0.0.0.0' resolves to '0.0.0.0';
120701 4:21:44 [Note] Server socket created on IP: '0.0.0.0'.
120701 4:21:44 [Note] Event Scheduler: Loaded 0 events
120701 4:21:44 [Note] mysqld: ready for connections.
Version: '5.5.25-log' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$
從日志信息上看,基本沒有什么大問題,除了一個warning內容。下面我們嘗試連接用戶。
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2011, 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> show databases;--連接成功,執行命令成功;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
說明MySQL運行狀態正常。下面是使用mysqladmin管理程序進行服務器狀態查詢和關閉操作。
mysql@bspdev mysql-5.5.25-linux2.6-i686]$mysqladmin -u root status
Uptime: 66 Threads: 1 Questions: 4 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.060
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$mysqladmin -u root shutdown
120701 4:23:16 [Note] mysqld: Normal shutdown
120701 4:23:16 [Note] Event Scheduler: Purging the queue. 0 events
120701 4:23:16 InnoDB: Starting shutdown...
120701 4:23:17 InnoDB: Shutdown completed; log sequence number 1595675
120701 4:23:17 [Note] mysqld: Shutdown complete
[1]+ Done mysqld
至此,我們起碼是將Mysql服務器安裝配置上,並且可以正常的進行訪問。但是,還有一些額外的問題沒有得到徹底解決,比如登錄密碼、告警日志中Warning信息等。
7、Warning警告分析
在實驗mysqld啟動的時候,我們看到了一條Warning告警信息。
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$ mysqld &
[1] 28398
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$120701 4:21:42 [Warning] Changed limits: max_open_files: 1024 max_connections: 151 table_cache: 431
120701 4:21:42 [Note] Plugin 'FEDERATED' is disabled.
雖然告警信息不是什么重要問題,但是還是需要稍微關注一下。
從提示內容上看,似乎是關於用戶連接數、同時打開文件數目和緩存的一些內容,存在限制。這讓我們想起了Oracle在安裝時,進行的操作系統用戶限制開啟過程。查閱資料后,只需要將mysql用戶的限制開啟就可以了。
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$ ulimit -Sa | grep "open files"
open files (-n) 1024
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$ ulimit -Ha | grep "open files"
open files (-n) 1024
[mysql@bspdev mysql-5.5.25-linux2.6-i686]$ su - root
Password:
--修改/etc/security/limits.conf文件內容;
[root@bspdev ~]# vi /etc/security/limits.conf
# /etc/security/limits.conf
#
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4
#添加內容;
mysql hard nofile 8192
mysql soft nofile 1200
再嘗試開啟服務器,warning信息消失。
[root@bspdev ~]# su - mysql
[mysql@bspdev ~]$ mysqld &
[1] 16285
[mysql@bspdev ~]$ 120701 4:26:21 [Note] Plugin 'FEDERATED' is disabled.
120701 4:26:21 InnoDB: The InnoDB memory heap is disabled
120701 4:26:21 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
120701 4:26:21 InnoDB: Compressed tables use zlib 1.2.3
120701 4:26:21 InnoDB: Using Linux native AIO
(篇幅原因,有省略……)
[mysql@bspdev ~]$ ps -ef | grep mysqld
mysql 16285 16257 1 04:26 pts/0 00:00:00 mysqld
mysql 16304 16257 0 04:26 pts/0 00:00:00 grep mysqld
8、用戶密碼問題
本地連接成功之后,筆者嘗試使用遠程連接。結果發現linux版本的一些安全限制。
--從遠程機器連接;
C:\Users\51ibm>mysql -h 192.168.137.89 -u root
ERROR 1130 (HY000): Host '192.168.137.1' is not allowed to connect to this MySQL
server
此時,MySQL服務器端日志記錄錯誤信息。
[Warning] IP address '192.168.137.1' could not be resolved: Temporary failure in name resolution
從信息上看,應該是登錄存在一些限制內容。
[mysql@bspdev ~]$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2011, 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> use mysql
Database changed
mysql> select host, name, password from user;
ERROR 1054 (42S22): Unknown column 'name' in 'field list'
mysql> select host, user, password from user;
+--------------------+------+----------+
| host | user | password |
+--------------------+------+----------+
| localhost | root | |
| bspdev.localdomain | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| bspdev.localdomain | | |
+--------------------+------+----------+
6 rows in set (0.00 sec)
mysql>
注意,在權限表中,沒有針對其他ip登陸的許可權限。說明Linux版本默認情況下,允許本地登陸,沒有密碼要求。但是不允許用戶從其他客戶端進行登陸。
我們需要人工的加以授權。
mysql> grant all on *.* to root@'%' identified by 'root' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> select host, user, password from user;
+--------------------+------+-------------------------------------------+
| host | user | password |
+--------------------+------+-------------------------------------------+
| localhost | root | |
| bspdev.localdomain | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| bspdev.localdomain | | |
| % | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+--------------------+------+-------------------------------------------+
7 rows in set (0.00 sec)
此時,我們可以從非服務器進行登陸,而且必須輸入密碼。
--不輸入用戶密碼登錄,被拒絕;
C:\Users\51ibm>mysql -h 192.168.137.89 -u root
ERROR 1045 (28000): Access denied for user 'root'@'192.168.137.1' (using passwor
d: NO)
--輸入root用戶密碼,通過;
C:\Users\51ibm>mysql -h 192.168.137.89 -u root -proot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2011, 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>
額外說明,從那張user表中,我們可以構建這樣的權限體系。相同的用戶名,從不同的機器上登陸時使用不同的密碼。
9、Linux服務化
在Windows中,我們是將MySQL作為一項服務,添加在服務列表中進行啟動關閉管理。在Linux中,我們也可以使用相同的配置內容。
--首先關閉mysqld服務
[mysql@bspdev ~]$ ps -ef | grep mysqld
mysql 16285 1 0 04:26 ? 00:00:00 mysqld
mysql 16475 16442 0 04:37 pts/2 00:00:00 grep mysqld
[mysql@bspdev ~]$ mysqladmin -u root shutdown
[mysql@bspdev ~]$ ps -ef | grep mysqld
mysql 16481 16442 0 04:38 pts/2 00:00:00 grep mysqld
[mysql@bspdev ~]$
將mysqld作為服務添加到列表中。
[root@bspdev ~]# cd /mysql/mysql-5.5.25-linux2.6-i686/support-files/
[root@bspdev support-files]# cp mysql.server /etc/rc.d/init.d/mysqld
[root@bspdev support-files]# chmod 700 /etc/init.d/mysqld
[root@bspdev support-files]# chkconfig --add mysqld
[root@bspdev support-files]# chkconfig --level 345 mysqld on
之后,我們就可以通過service mysqld start來啟動MySQL數據庫了。
[root@bspdev support-files]#service mysqld start
Starting MySQL...[ OK ]
[root@bspdev support-files]# mysql -u root
-bash: mysql: command not found
[root@bspdev support-files]# su - mysql
[mysql@bspdev ~]$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2011, 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>
對service命令,我們還可以使用如status、stop等命令。
[root@bspdev ~]# service mysqld status
MySQL running (16839)[ OK ]
[root@bspdev ~]# service mysqld stop
Shutting down MySQL.[ OK ]
[root@bspdev ~]# ps -ef | grep mysqld
root 16988 16902 0 04:44 pts/2 00:00:00 grep mysqld
[root@bspdev ~]#
10、結論
作為目前最流行的開源數據庫產品,MySQL是比較成功的。學習安裝MySQL,只是學習的一個開始入手點。之后,還有很多的問題需要解決和學習,需要不斷的堅持和努力才能完成。
----整理自網上
