我从存储库安装了 mysql-server,并想将 datadir 更改为自定义位置。将 datadir 设置为 /var/lib/mysql 后,我能够启动服务器,但对于新位置,我收到错误。
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
以下是对 my.cnf 的更改:
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /media/db2/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
我搜索了这个网站和其他网站,偶然发现了此链接并修改了我的 /etc/apparmor.d/usr.sbin.mysqld 文件。以下是相关更改
# /var/lib/mysql/ r,
# /var/lib/mysql/** rwk,
/media/db2/mysql/ r,
/media/db2/mysql/** rwk,
然后我就可以重新启动mysql服务了
sudo service mysql restart
但无法打开mysql
mysql -u root -p
我又遇到了一个与上述类似的错误。因此我又搜索了一些内容,并将文件从 /var/lib/mysql 复制到了我的新目录中。
现在,当我尝试重新启动 mysql 时,它就挂起了。/var/log/mysql/error.log 中的日志文件显示:
130125 12:35:41 InnoDB: Waiting for the background threads to start
130125 12:35:42 InnoDB: 1.1.8 started; log sequence number 1595675
130125 12:35:42 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
130125 12:35:42 [Note] - '127.0.0.1' resolves to '127.0.0.1';
130125 12:35:42 [Note] Server socket created on IP: '127.0.0.1'.
130125 12:35:42 [ERROR] /usr/sbin/mysqld: Can't find file: './mysql/host.frm' (errno: 13)
130125 12:35:42 [ERROR] Fatal error: Can't open and lock privilege tables: Can't find file: './mysql/host.frm' (errno: 13)
130125 12:35:43 [Note] Plugin 'FEDERATED' is disabled.
/usr/sbin/mysqld: Can't find file: './mysql/plugin.frm' (errno: 13)
130125 12:35:43 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
130125 12:35:43 InnoDB: The InnoDB memory heap is disabled
130125 12:35:43 InnoDB: Mutexes and rw_locks use GCC atomic builtins
130125 12:35:43 InnoDB: Compressed tables use zlib 1.2.7
130125 12:35:43 InnoDB: Using Linux native AIO
130125 12:35:43 InnoDB: Initializing buffer pool, size = 128.0M
130125 12:35:43 InnoDB: Completed initialization of buffer pool
130125 12:35:43 InnoDB: highest supported file format is Barracuda.
InnoDB: Log scan progressed past the checkpoint lsn 49439
130125 12:35:43 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
InnoDB: Doing recovery: scanned up to log sequence number 1595675
130125 12:35:45 InnoDB: Starting an apply batch of log records to the database...
InnoDB: Progress in percents: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
InnoDB: Apply batch completed
我认为问题在于 appamor,而且它基本上是一个权限问题......
这是我希望数据库文件所在位置的权限......
$ ls /media/db2/mysql -al
total 28732
drwxrwxrwx 3 mysql root 4096 Jan 25 12:39 .
drwxr-xr-x 4 root root 4096 Jan 25 2013 ..
-rw-rw---- 1 mysql mysql 18874368 Jan 25 2013 ibdata1
-rw-rw---- 1 mysql mysql 5242880 Jan 25 12:39 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 Jan 25 2013 ib_logfile1
drwx------ 2 root root 4096 Jan 25 2013 mysql
任何帮助都将不胜感激。谢谢!
答案1
我可以通过改变 datadir 文件夹的所有者,让 mysql 用户成为所有者来解决此问题
sudo chown mysql:mysql /mysql/data/dir
不确定这是否能为其他人解决问题,但对我来说有效。