:cry: 请原谅我使用java代码的风格来表示很多的终端命令,编辑器自带的样式太少了⋯⋯


[b]先是给mysql配置~[/b]
(文中"/Users/XXX"代表当前用户主目录,我是在自己主目录下建了一个 Dev 目录,各种开发工具都放在里面,下文中的路径请根据自己实际目录作调整)

1、这里使用官网的tar.gz包,然后解压到 /Users/XXX/Dev/mysql 目录下

2、在 /Users/XXX/Dev/mysql/data 下新建目录 data1 和 data2

3、初始化数据库,在 terminal 执行如下shell :

[/code]
cd /Users/XXX/Dev/mysql
./script/mysql_install_db --datadir=./data/data1
./script/mysql_install_db --datadir=./data/data2



4、mac下mysql默认是不需要 my.cnf 的,所以需要在 /Users/XXX/Dev/mysql 下新建 my.cnf 内容如下:


[mysqld_multi]
mysqld = /Users/XXX/Dev/mysql/bin/mysqld_safe
mysqladmin = /Users/XXX/Dev/mysql/bin/mysqladmin
user = root
#password = root

[mysqld1]
port      = 3307
socket    = /tmp/mysql.sock1
pid-file=/Users/XXX/Dev/mysql/data/data1/db-app1.pid
log=/Users/XXX/Dev/mysql/data/data1/db-app.log
datadir = /Users/XXX/Dev/mysql/data/data1
basedir = /Users/XXX/Dev/mysql
user = root

[mysqld2]
port = 3308
socket = /tmp/mysql.sock2
pid-file = /Users/XXX/Dev/mysql/data/data2/db-app2.pid
log=/Users/XXX/Dev/mysql/data/data2/db-app.log
datadir = /Users/XXX/Dev/mysql/data/data2
basedir = /Users/XXX/Dev/mysql
user = root

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout




5、手动启动/关闭实例的命令(这里做测试用,后期改为自动启动将命令改为脚本执行)



启动:


/Users/XXX/Dev/mysql/bin/mysqld_multi --defaults-file=/Users/XXX/Dev/mysql/my.cnf start 1-2



注意:有些文章讲的是 –config-file ,但在5.5的版本里,已经没有该选项了。


起动时,会报找不到my_print_defaults。需要在PATH中添加bin目录。



关闭:


/Users/XXX/Dev/mysql/bin/mysqld_multi --defaults-file=/Users/XXX/Dev/mysql/my.cnf stop 1-2



或者


/Users/XXX/Dev/mysql/bin/mysqladmin -uroot -h 127.0.0.1 -P 3306 shutdown



其实mysqld_multi就是通过mysqladmin来实现关闭实例的。



############## 环境变量设置方法1 #################
打开shell后, 默认在$HOME目录下
键入: vi .bash_profile 回车, 编辑.bash_profile文件
按i进入插入文字编辑模式
找到设定PATH环境变量的一行,将你要添加的完整路径(如:/Users/XXX/Dev/mysql/bin)加入其中。如果没有找到设定PATH变量的行, 你可以自己添加一行:

export PATH=${PATH}:/Users/XXX/Dev/mysql/bin
############ 环境变量设置方法2(推荐) ##############
在 /etc/paths.d 建立一个文件专门表示mysql的path
1)浏览到 /etc/paths.d :
cd /etc/paths.d
2)创建文件:
sudo vi mysql
3)编辑模式下输入:
/Users/XXX/Dev/mysql/bin
4)可以在terminal下输入以下命令验证是否生效:
echo $PATH



6、修改root密码


mysqladmin -u root -h 127.0.0.1 -P 3307 password [yourpassword]
mysqladmin -u root -h 127.0.0.1 -P 3308 password [yourpassword]





20120229 补充:


使用mysqldump时需指定sock,比如备份3308端口的数据库时,须添加“ -S /tmp/mysql.sock2”



=================[b]到这里mysql配置完毕,开始设置开机自动启动[/b]=====================



主要做法是将脚本加入到StartupItems



1.首先,要放到 /Library/StartupItems 下的自定义启动项必须是一个目录,然后再把脚本放到目录下,而且脚本与目录必须同名,以启动项命名为 MysqlMulti 为例


terminal下执行:


cd /Library/StartupItems
sudo mkdir MysqlMulti



2.创建启动脚本


创建脚本文件:


sudo vi /Library/StartupItems/MysqlMulti/MysqlMulti



编辑模式下输入以下内容:


#!/bin/sh

# Source common setup, including hostconfig.
#
. /etc/rc.common

StartService( )
{
	ConsoleMessage "Starting MySQL"
	/Users/XXX/Dev/mysql/bin/mysqld_multi --defaults-file=/Users/XXX/Dev/mysql/my.cnf start 1-2
}

StopService( )
{
	ConsoleMessage "Stopping MySQL"
	/Users/XXX/Dev/mysql/bin/mysqld_multi --defaults-file=/Users/XXX/Dev/mysql/my.cnf stop 1-2
}

RestartService( )
{
	ConsoleMessage "Restarting MySQL"
	StopService
	StartService
}

RunService "$1"




3.启动脚本描述、参数


创建描述文件:


sudo vi /Library/StartupItems/MysqlMulti/StartupParameters.plist



编辑模式下输入:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
    <dict>
        <key>Description</key>
        <string>MysqlMulti</string>
        <key>OrderPreference</key>
        <string>Late</string>
        <key>Provides</key>
        <array>
                <string>Launch mysql at startup</string>
        </array>
    </dict>
</plist>




至此,使用ls命令你将看到:


$ls /Library/StartupItems/MysqlMulti/
MysqlMulti               StartupParameters.plist




4.修改权限


sudo chown -R root:wheel /Library/StartupItems/MysqlMulti
sudo chmod -R 755 /Library/StartupItems/MysqlMulti



5.


当设置完成后,可以在终端上用SystemStarter 来调试配置的信息


命令为:SystemStarter -n -D


根据提示的信息可以查看配置的问题。



6.启动时可能提示:


WARNING: my_print_defaults command not found.


Please make sure you have this command available and


in your path. The command is available from the latest


MySQL distribution.



查看 /Users/XXX/Dev/mysql/support-files/mysql.server 发现以下注释:


# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#|
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]|
#   basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.



原来 my_print_defaults 需要拷贝到 /usr/bin ,干脆使用更方便的软连接:


sudo ln -s ~/Dev/mysql/bin/my_print_defaults /usr/bin/my_print_defaults



问题解决