在诸多开源数据库中,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:Users51ibm>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:Users51ibm>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:Users51ibm>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,只是学习的一个开始入手点。之后,还有很多的问题需要解决和学习,需要不断的坚持和努力才能完成。
----整理自网上