Ubuntu 10.10下更改mysql的datadir

正常情况下的做法:修改/etc/apparmor.d/usr.sbin.mysqld的做法,一般能够解决常见的问题,如果还不能成功,比如我在Ubuntu 10.10(5.1.49-1ubuntu8.1)下却没有效果,可以试验下以下所描述的办法。

在网上搜了很久都没有合适的解决办法,官方论坛上也有人报告这个问题,但没有解决的建议,cannot move mysql data directory to another partition ,错误信息为:

1
2
3
4
5
6
7
8
9
10
110127 20:46:04 [Note] Plugin 'FEDERATED' is disabled.
mysqld: Can 't find file: ' . /mysql/plugin .frm' (errno: 13)
110127 20:46:04 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgr
ade to create it.
110127 20:46:04  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name . /ibdata1
InnoDB: File operation call: 'create' .
InnoDB: Cannot continue operation.

或者是:/usr/sbin/mysqld: can’t find file: ‘./mysql/plugin.frm 我无意中解决了这个问题 :)
感谢 http://bbs.php100.com/read-htm-tid-49630.html 给了我灵感。

基本思路:更改文件权限基本上不行,我查了/etc/apparmor.d下面的文件,也考虑了类似usr.bin.firefox的做法,把usr.sbin.mysqld软链到/etc/apparmor.d/disable下面,或者把apparmor都停止掉,重启mysql还是一样的错误。

干脆,重新建立mysql的库文件吧,用msql自身的命令来更改datadir的位置,这样总该可以了吧。以下是步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root@T410i:~ # mysql_install_db --basedir =/usr --datadir=/home/mysql --user=mysql
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:
 
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h T410i password 'new-password'
 
Alternatively you can run:
/usr/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 /usr ; /usr/bin/mysqld_safe &
 
You can test the MySQL daemon with mysql- test -run.pl
cd /usr/mysql-test ; perl mysql- test -run.pl
 
Please report any problems with the /usr/scripts/mysqlbug script!

这样可以修改mysql的配置文件了,用Ubuntu原来的方式进行启动,省力,省心。 ;)

1
2
3
4
root@T410i:~ # vi /etc/mysql/my.cnf
 
#datadir                = /var/lib/mysql
datadir         = /home/mysql

需要重建root的密码,否则登录会报错,如下面的输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
root@T410i:~ # mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root' @ 'localhost' (using password: YES)
 
root@T410i:~ # mysqladmin -u root password 'root'
root@T410i:~ # mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.49-1ubuntu8.1 (Ubuntu)
 
Copyright (c) 2000, 2010, Oracle and /or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

建立一个测试库,查看数据文件的位置,检验是否更换成功。

1
2
3
4
5
6
7
8
mysql> create database test ;
Query OK, 1 row affected (0.00 sec)
 
mysql> exit
Bye
 
root@T410i:~ # ls /home/mysql
ibdata1  ib_logfile0  ib_logfile1  mysql  T410i.pid  test

大功告成,可以把/var/lib/mysql的数据文件都移走吧,可以把/分区给节省下来,我有一个变态库有8G多啊,终于解放了。

你可能感兴趣的:(Ubuntu 10.10下更改mysql的datadir)