ubuntu8.04~8.10安装默认的mysql,不能运行mysql_install_db,不能使用mysqld_multi的罪魁是AppArmor,什么是AppArmor(
传送门
),不能访问的出错结果是因为apparmor控制了对multi实例的数据目录的访问权限
出错代码:
>> sudo mysql_install_db --datadir
=
/var/lib/mysql1 --no-defaults --user=mysql
Installing MySQL system tables...
100304 22 : 34 : 18 [ Warning ] Can't create test file /var/lib/mysql1/PB.lower-test
100304 22 : 34 : 18 [ Warning ] Can't create test file /var/lib/mysql1/PB.lower-test
ERROR: 1005 Can't create table 'db' ( errno: 13 )
100304 22 : 34 : 18 [ ERROR ] Aborting
Installing MySQL system tables...
100304 22 : 34 : 18 [ Warning ] Can't create test file /var/lib/mysql1/PB.lower-test
100304 22 : 34 : 18 [ Warning ] Can't create test file /var/lib/mysql1/PB.lower-test
ERROR: 1005 Can't create table 'db' ( errno: 13 )
100304 22 : 34 : 18 [ ERROR ] Aborting
解决办法:
编辑/etc/apparmor.d/usr.sbin.mysqld这个文件,可以看到默认apt-get安装的mysql对应的数据目录和日志以及socket,pid文件的权限都是存在的,如法炮制,修改配置文件如下:
/
etc
/
mysql
/*
.pem r,
/ etc / mysql / conf.d / r,
/ etc / mysql / conf.d /* r,
/ etc / mysql / my.cnf r,
/ usr / sbin / mysqld mr,
/ usr / share / mysql /** r,
/ var / log / mysql.log rw,
/ var / log / mysql.err rw,
/ var / lib / mysql / r,
/ var / lib / mysql /** rwk,
/ var / log / mysql / r,
/ var / log / mysql /* rw,
/ var / run / mysqld / mysqld.pid w ,
/ var / run / mysqld / mysqld.sock w ,
/ var / log / mysql1.log rw,
/ var / log / mysql1.err rw,
/ var / lib / mysql1 / r,
/ var / lib / mysql1 /** rwk,
/ var / log / mysql1 / r,
/ var / log / mysql1 /* rw,
/ var / run / mysqld / mysqld1.pid w ,
/ var / run / mysqld / mysqld1.sock w ,
/ var / log / mysql2.log rw,
/ var / log / mysql2.err rw,
/ var / lib / mysql2 / r,
/ var / lib / mysql2 /** rwk,
/ var / log / mysql2 / r,
/ var / log / mysql2 /* rw,
/ var / run / mysqld / mysqld2.pid w ,
/ var / run / mysqld / mysqld2.sock w ,
/ etc / mysql / conf.d / r,
/ etc / mysql / conf.d /* r,
/ etc / mysql / my.cnf r,
/ usr / sbin / mysqld mr,
/ usr / share / mysql /** r,
/ var / log / mysql.log rw,
/ var / log / mysql.err rw,
/ var / lib / mysql / r,
/ var / lib / mysql /** rwk,
/ var / log / mysql / r,
/ var / log / mysql /* rw,
/ var / run / mysqld / mysqld.pid w ,
/ var / run / mysqld / mysqld.sock w ,
/ var / log / mysql1.log rw,
/ var / log / mysql1.err rw,
/ var / lib / mysql1 / r,
/ var / lib / mysql1 /** rwk,
/ var / log / mysql1 / r,
/ var / log / mysql1 /* rw,
/ var / run / mysqld / mysqld1.pid w ,
/ var / run / mysqld / mysqld1.sock w ,
/ var / log / mysql2.log rw,
/ var / log / mysql2.err rw,
/ var / lib / mysql2 / r,
/ var / lib / mysql2 /** rwk,
/ var / log / mysql2 / r,
/ var / log / mysql2 /* rw,
/ var / run / mysqld / mysqld2.pid w ,
/ var / run / mysqld / mysqld2.sock w ,
修改完这个文件后sudo /etc/init.d/apparmor restart,重启apparmor
继续运行 sudo mysql_install_db –datadir=/var/lib/mysql1 –no-defaults –user=mysql
这样就能安装mysql的系统数据文件到/var/lib/mysql1下面了
配置多个实例很简单:编辑/etc/mysql/my.cnf
[
mysqld_multi
]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root
password = 123
[ mysqld1 ]
pid-file = /var/run/mysqld/mysqld1.pid
socket = /var/run/mysqld/mysqld1.sock
port = 3306
datadir = /var/lib/mysql1
user = mysql
log = /var/log/mysql1.log
[ mysqld2 ]
pid-file = /var/run/mysqld/mysqld2.pid
socket = /var/run/mysqld/mysqld2.sock
port = 3307
datadir = /var/lib/mysql2
user = mysql
log = /var/log/mysql2.log
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root
password = 123
[ mysqld1 ]
pid-file = /var/run/mysqld/mysqld1.pid
socket = /var/run/mysqld/mysqld1.sock
port = 3306
datadir = /var/lib/mysql1
user = mysql
log = /var/log/mysql1.log
[ mysqld2 ]
pid-file = /var/run/mysqld/mysqld2.pid
socket = /var/run/mysqld/mysqld2.sock
port = 3307
datadir = /var/lib/mysql2
user = mysql
log = /var/log/mysql2.log
运行sudo mysqld_multi start 1,2 –no-log,就能见到终端输出正确的启动信息了
~/home/pb$ netstat -lt | grep 33
tcp 0 0 *:3307 *:* LISTEN
tcp 0 0 *:3308 *:* LISTEN
tcp 0 0 *:3307 *:* LISTEN
tcp 0 0 *:3308 *:* LISTEN