基于OpenBSD的MySQL服务器

在OpenBSD环境下搭建各种服务器的相关讨论。

版主: lionuxchenjun天地乾坤

回复
头像
leo
帖子: 2465
注册时间: 2010-01-21 3:27

基于OpenBSD的MySQL服务器

帖子 leo » 2010-01-21 11:56

这里先简要地介绍一下,MySQL:
MySQL是最流行的开放源码SQL数据库管理系统,它是由MySQL AB公司开发、发布并支持的。MySQL AB是由多名MySQL开发人创办的一家商业公司。它是一家第二代开放源码公司,结合了开放源码价值取向、方法和成功的商业模型。
在MySQL的网站(http://www.mysql.com/)上,给出了MySQL的最新信息。
好了,我们步入正题:
先说一下我的安装环境:OpenBSD 4.6稳定版,i386平台,shell采用系统默认的ksh,未安装任何的package,为了节省时间我们采用Packages的安装方式,这也是OpenBSD官方推荐的第三方软件安装模式,这可以极大的缩短安装时间,节省系统资源,当然如果你需要控制编译过程,例如增加或删除某些选项,定制编译方式,你也可以采用ports的方式安装。我们这里的设置的Packages路径为OpenBSD官方FTP服务器,以下是具体步骤,如果你使用的不是ksh请根据自己的shell:
一 . 设置packages路径:

代码: 全选

# export PKG_PATH=ftp://ftp.openbsd.org/pub/OpenBSD/4.6/packages/i386/ 
二 . 安装MySQL服务器:

代码: 全选

# pkg_add mysql-server 
mysql-client-5.0.83: complete 
p5-DBD-mysql-4.010:p5-Net-Daemon-0.43: complete 
p5-DBD-mysql-4.010:p5-PlRPC-0.2018p0: complete 
p5-DBD-mysql-4.010:p5-DBI-1.607: complete 
p5-DBD-mysql-4.010: complete 
mysql-server-5.0.83: complete 
--- mysql-server-5.0.83 ------------------- 
You can find detailed instructions on how to install a database 
in /usr/local/share/doc/mysql/README.OpenBSD. 

安装完MySQL server后,会自动在系统上创建一个_mysql用户,让我们先来看一下:

代码: 全选

# userinfo _mysql 
login _mysql 
passwd ************* 
uid 502 
groups _mysql 
change NEVER 
class daemon 
gecos MySQL Account 
dir /nonexistent 
shell /sbin/nologin 
expire NEVER 

从安装过程显示信息看,不但安装了mysql服务器,还安装了相关的一些依赖包,最后建议我们阅读/usr/local/share/doc/mysql/README.OpenBSD这个帮助文档以便熟悉如何在OpenBSD系统上设置MySQL服务器。

三 . 创建默认的数据库:
因为我们是在系统上第一次安装MySQL,需要用下列命令先创建一个默认的数据库:

代码: 全选

# /usr/local/bin/mysql_install_db 
四 . 启动数据库:

代码: 全选

# mysqld_safe & 
这里说明一下,最后的 & 符号是让mysql进程在后台运行,如果你在VMWARE里运行时MySQL无法切换到后台,直接按CTRL+C断开,此时MYSQL会仍在后台运行,不必担心。
五 . 设定数据库本地root密码
我们这里这里密码是9971us,读者请根据自己的情况调整:

代码: 全选

# mysqladmin -u root password '9971us' 
六 . 设定数据库远程root密码
我们这里这里密码是9971us,读者请根据自己的情况调整:

代码: 全选

# mysqladmin -u root -h test.gobsd.org password '9971us' 
我们这里稍微解释一些,所谓的远程密码就是指从另一台计算机上登录到MYSQL数据库时的密码,也就是说从远程的web服务器上调用此计算机上的数据库,你还需要设置本地的host文件,MySQL加密通过TCP连接发送的登录信息。
七 . 设置MySQL的配置文件my.cnf
这里还需要大致地说一下,在/usr/local/share/mysql/目录里包含了一系列MySQL的简单的配置文件:
my-small.cnf 内存小于64M仅运行MySQL服务器的主机使用这个配置文件。
my-medium.cnf 内存在32-64M之间,主要运行MySQL服务,或128M以上,MySQL和web服务器一起运行时使用这个配置文件。
my-large.cnf 内存是512M的重要运行MySQL使用这个配置文件。
my-innodb-heavy-4G.cnf 内存是1-2G,主要运行MySQL使用这个配置文件。
my-huge.cnf 内存4G以上,主要运行MySQL的主机使用这个配置文件。
读者请根据自己的情况用上述文件中的一个替换 /etc/my.cnf文件,如果你有特殊的需要,例如要修改socket或端口请自行调整。
我的主机是是512M,用my-media.cnf,所以使用下面的命令替换原来的My.cnf文件:

代码: 全选

# cp /usr/local/share/mysql/my-medium.cnf /etc/my.cnf 
有一点需要提醒读者的是,如果你的MySQL仅用于基于PHP的web服务器,或者说你的MYSQL数据库和WEB服务器在一台主机上,那么禁用TCP连接对安装MySQL来说是比较安全的。
具体做法是编辑 /etc/my.cnf 文件,将里面的内容

代码: 全选

# skip-networking 
前面的注释符去掉变成这样:

代码: 全选

 skip-networking 
然后保存退出。
八 . 在login.conf里增加相关项:
默认情况下,用户_mysql和mysqld进程运行的登录级别是"daemon",在一台非常繁忙的服务器上,将让用户_mysql和mysqld进程运行在自己的登录级别上很明智。
例如调整打开文件等,你必须将下列内容加入到/etc/login.conf文件内,以便登录时mysql有自己的登录级别,否则其无法运行。

代码: 全选

 mysql:\ 
:openfiles-cur=1024:\ 
:openfiles-max=2048:\ 
:tc=daemon: 
然后重建login.conf.db文件

代码: 全选

# cap_mkdb /etc/login.conf 
九 . 设置MySQL自动启动
在/etc/rc.local里加入如下内容:

代码: 全选

 if [ -x /usr/local/bin/mysqld_safe ] ; then 
su -c mysql root -c '/usr/local/bin/mysqld_safe >/dev/null 2>&1 &' 
echo -n ' mysql' 
fi 


到这一步,我们重新启动一下系统,看看MySQL的设置是否正常.
十 . MySQL系统使用
重新启动主机后,我们输入命令:

代码: 全选

# top
load averages: 0.08, 0.22, 0.20 00:31:27
32 processes: 31 idle, 1 on processor
CPU states: 0.0% user, 0.0% nice, 0.3% system, 0.0% interrupt, 99.7% idle
Memory: Real: 38M/228M act/tot Free: 1328M Swap: 0K/502M used/tot
PID USERNAME PRI NICE SIZE RES STATE WAIT TIME CPU COMMAND
5309 _mysql 2 0 42M 19M sleep poll 0:03 0.00% mysqld
32211 _pflogd 4 0 520K 312K sleep bpf 0:00 0.00% pflogd
8487 www 2 0 2676K 5892K sleep select 0:00 0.00% httpd
11083 root 2 0 1204K 1720K sleep select 0:00 0.00% sendmail
23822 www 2 0 2760K 5728K idle netcon 0:04 0.00% httpd
30376 www 2 0 2744K 5716K idle netcon 0:01 0.00% httpd
16414 root 2 0 520K 824K idle select 0:00 0.00% cron
16390 www 2 0 2736K 5724K idle netcon 0:00 0.00% httpd
6560 www 2 0 2740K 5728K idle netcon 0:00 0.00% httpd
11780 _syslogd 2 0 552K 692K sleep poll 0:00 0.00% syslogd
7460 www 2 0 2780K 5832K idle netcon 0:00 0.00% httpd
22958 root 2 0 3272K 2452K sleep select 0:00 0.00% sshd
16482 www 2 0 2704K 5072K idle netcon 0:00 0.00% httpd
30166 www 2 0 2700K 5068K idle netcon 0:00 0.00% httpd
22111 www 2 0 2704K 5000K idle netcon 0:00 0.00% httpd
9266 root 2 0 684K 1188K idle select 0:00 0.00% sshd
8319 _dhcp 2 0 348K 352K idle poll 0:00 0.00% dhclient
17232 root 31 0 392K 1176K onproc - 0:00 0.00% top

大家看到了吧,这里面有一个mysqld,说明mysql已经运行了,为了方便拷贝屏幕内容以便给读者做解释,我是远程连接上的MySQl所在主机,
实际上如果你在本地启动时就可以看到启动信息里,有一行:

代码: 全选

mysqlstandard daemons:cron

这说明mysql已经启动了。
如果你的mysql没有启动,请仔细检查一下上面的步骤,看看有没有遗漏,如果有请不上,如果还不行试试这样:
先建立一个目录 /var/run/mysql/,并将其所属用户及组设置成_mysql,否则后面重新启动时MySQL无法启动:

代码: 全选

# midir -p /var/run/mysql 
# chown -R _mysql:_mysql /var/run/mysql/
这个应该是mysql自动建立的,你如果没有这个目录,手动建立一个再试试。还有一点需要注意的是,看一下你的/tmp目录,它的属性应该是777,也即是像这样:

代码: 全选

drwxrwxrwt 4 root wheel 512 Dec 19 17:58 tmp

如果你的文件权限不是这样,改改试试。
好了,现在我们测试一下mysql服务器:

代码: 全选

# mysql -p 
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 4 
Server version: 5.0.83-log OpenBSD port: mysql-server-5.0.83 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
登录成功输入命令,注意是以;结尾的。:

代码: 全选

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.04 sec)
 
建立一个数据库
好的,现在我们试试建立一个数据库,先建立一个名为9971db的数据库:

代码: 全选

mysql> create database 9971db; 
Query OK, 1 row affected (0.01 sec) 
添加数据库用户
再向该数据库添加一个用户全权用户test1:

代码: 全选

mysql> grant all on 9971db.* to test1@localhost identified by 'testpw'; 
Query OK, 0 rows affected (0.01 sec) 
这里注意grant后面跟着的参数是all,它表示赋予这个用户所有操作数据库的权限,当然你也可以用下列权限中的一个替换它:
select
update
delete
create
drop
那怎样删除一个用户呢?
删除用户
例如我们想从数据库9971db中删除我们刚建立的test1用户,执行下列操作:

代码: 全选

mysql> revoke all privileges on 9971db.* from test1@localhost;Query OK, 0 rows affected (0.00 sec) 
这样我们刚建立的这个test1用户就从9971db这个数据库中删除了。
因为同一个用户可以使用MySQL里的很多不同的数据库,所以有时你可能要将这个用户从MySQL里所有的数据库中删除,这样:

代码: 全选

mysel> revoke all privileges,grant option from test1@localhost; 
mysql> drop test1 test1@localhost; 
删除一个数据库
例如我们要删除一个名为obtest的数据库:

代码: 全选

mysql> drop database obtest; 
备份数据库
假设我们想备份刚建立的9971db这个数据库的数据备份到 /home 下,在shell里:

代码: 全选

# mysqldump -u root -p 9971db > /home/9971db.sql 
Enter password:9971us 
# 
这里面的9971.sql是备份文件的名称。
恢复数据库
如果想还原这个数据库,就这样:

代码: 全选

# mysql -u root -p 9971db < /home/9971db.sql 
Enter password:9971us 
# 
好了,对单个数据库的操作来说,我们基本上走了一遍,读者如果还想需要知道更多的操作指令请参看mysql的说明。

不过还没有完,最后还有一项工作是:
十一 . 执行mysql安全脚本:

代码: 全选

 
# /usr/local/bin/mysql_secure_installation 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL 
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! 
 
In order to log into MySQL to secure it, we'll need the current 
password for the root user. If you've just installed MySQL, 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 MySQL 
root user without the proper authorisation. 
You already have a root password set, so you can safely answer 'n'. 
Change the root password? [Y/n] n 这里问你是不是换root密码,我们刚设定的,所以不用换。 
... skipping. 
By default, a MySQL installation has an anonymous user, allowing anyone 
to log into MySQL 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 [color=red]这个匿名帐号自然要删除 [/color]
... Success! 
Normally, root should only be allowed to connect from 'localhost'. This 
ensures that someone cannot guess at the root password from the network. 
Disallow root login remotely? [Y/n] n [color=red]这个自己根据情况,如果你要远程登录选n.[/color] 
... skipping. 
By default, MySQL 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 [color=red]留着test数据库也没有用 [/color]
- 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 [color=red]重新载入权限表 [/color]
... Success! 
Cleaning up... 
 
All done! If you've completed all of the above steps, your MySQL 
installation should now be secure. 
Thanks for using MySQL! 
 
#

上面就是删除了mysql默认建立的test数据库和匿名帐号。
到此为止,关于在OpenBSD上安装MySQL服务器的基本操作就算完成了,更详细的信息还请您自行参考mysql的主页。
附录:
了解MySQL命令:

代码: 全选

mysql> [color=deepskyblue]help [/color]
For information about MySQL products and services, visit: 
[URL]http://www.mysql.com/[/URL] 
For developer information, including the MySQL Reference Manual, visit: 
[URL]http://dev.mysql.com/[/URL] 
To buy MySQL Network Support, training, or other products, visit: 
[URL]https://shop.mysql.com/[/URL] 
List of all MySQL commands: 
Note that all text commands must be first on line and end with ';' 
? (\?) Synonym for `help'. 
clear (\c) Clear the current input statement. 
connect (\r) Reconnect to the server. Optional arguments are db and host. 
delimiter (\d) Set statement delimiter. 
edit (\e) Edit command with $EDITOR. 
ego (\G) Send command to mysql server, display result vertically. 
exit (\q) Exit mysql. Same as quit. 
go (\g) Send command to mysql server. 
help (\h) Display this help. 
nopager (\n) Disable pager, print to stdout. 
notee (\t) Don't write into outfile. 
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. 
print (\p) Print current command. 
prompt (\R) Change your mysql prompt. 
quit (\q) Quit mysql. 
rehash (\#) Rebuild completion hash. 
source (\.) Execute an SQL script file. Takes a file name as an argument. 
status (\s) Get status information from the server. 
system (\!) Execute a system shell command. 
tee (\T) Set outfile [to_outfile]. Append everything into given outfile. 
use (\u) Use another database. Takes database name as argument. 
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. 
warnings (\W) Show warnings after every statement. 
nowarning (\w) Don't show warnings after every statement. 
For server side help, type 'help contents' [color=deepskyblue]这里告诉你服务器端的帮助输入help contenrs[/color] 
mysql> [color=deepskyblue]help contents[/color] 
You asked for help about help category: "Contents" 
For more information, type 'help <item>', where <item> is one of the following 
categories: 
Account Management 
Administration 
Compound Statements 
Data Definition 
Data Manipulation 
Data Types 
Functions 
Functions and Modifiers for Use with GROUP BY 
Geographic Features 
Language Structure 
Table Maintenance 
Transactions 
User-Defined Functions 
Utility 
比如我们要了解帐号的管理:

代码: 全选

mysql> [color=deepskyblue]help Account Management [/color]
You asked for help about help category: "Account Management" 
For more information, type 'help <item>', where <item> is one of the following 
topics: 
CREATE USER 
DROP USER 
GRANT 
RENAME USER 
REVOKE 
SET PASSWORD 
mysql> [color=deepskyblue]help CREATE USER[/color] 
Name: 'CREATE USER' 
Description: 
Syntax: 
CREATE USER user [IDENTIFIED BY [PASSWORD] 'password'] 
[, user [IDENTIFIED BY [PASSWORD] 'password']] ... 
The CREATE USER statement was added in MySQL 5.0.2. This statement 
creates new MySQL accounts. To use it, you must have the global CREATE 
USER privilege or the INSERT privilege for the mysql database. For each 
account, CREATE USER creates a new record in the mysql.user table that 
has no privileges. An error occurs if the account already exists. Each 
account is named using the same format as for the GRANT statement; for 
example, 'jeffrey'@'localhost'. If you specify only the user name part 
of the account name, a host name part of '%' is used. For additional 
information about specifying account names, see [HELP GRANT]. 
The account can be given a password with the optional IDENTIFIED BY 
clause. The user value and the password are given the same way as for 
the GRANT statement. In particular, to specify the password in plain 
text, omit the PASSWORD keyword. To specify the password as the hashed 
value as returned by the PASSWORD() function, include the PASSWORD 
keyword. See [HELP GRANT]. 
URL: [URL]http://dev.mysql.com/doc/refman/5.0/en/create-user.html[/URL] 
 
mysql> [color=deepskyblue]exit [/color]
Bye 
# 
读者也可以直接上MYSQL的中文主页上了解相关命令的使用,毕竟那里是咱们的母语,学习时掌握得更轻松。
或者去http://bbs.mysql.cn/index.php,人气似乎还很高。
我这里留一份中文的MySQL 5.1参考手册,不过是CHM格式的,下载地址在下面:
下载地址

leo@gobsd.org

天地乾坤
钾 K
帖子: 17
注册时间: 2010-02-27 13:23

帖子 天地乾坤 » 2010-03-01 6:47

虽然我本身不是特别喜欢Mysql,而且Mysql的前途未卜,但是他还是最实用,最时髦的,这可是好多人的基础数据库系统。

回复

在线用户

正浏览此版面之用户: 没有注册用户 和 33 访客