Ubuntu初始化MySQL碰到的坑

想着将MySQL初始化,改变一下存放数据文件的位置:

root@ubuntu:/lvmdata# mkdir data
root@ubuntu:/lvmdata# chown -R mysql:mysql /lvmdata/data

修改MySQL配置文件:

datadir = /lvmdata/data

然后初始化:

root@ubuntu:/lvmdata# mysqld --initialize  --user=mysql --datadir=/lvmdata/data

报错:

mysqld: Can't create directory '/lvmdata/data/' (Errcode: 17 - File exists)

好吧,我让你在data再建一个文件夹好了吧:

root@ubuntu:/lvmdata# mysqld --initialize  --user=mysql --datadir=/lvmdata/data/data
mysqld: Can't create directory '/lvmdata/data/data/' (Errcode: 13 - Permission denied)
2018-01-07T08:59:15.782022Z 0 [ERROR] Aborting

。。。

建好了你说已存在,没建好你说没权限创建,可是明明权限都给mysql用户了啊。

查了下,是这玩意:

因为Ubuntu有个AppArmor,是一个Linux系统安全应用程序,类似于Selinux,AppArmor默认安全策略定义个别应用程序可以访问系统资源和各自的特权,如果不设置服务的执行程序,即使你改了属主属组并0777权限,也是对服务起不到作用。

ok,apt安装下MySQL默认数据目录是/var/lib/mysql,其它的目录权限都不可。开始修改:

# vim /etc/apparmor.d/usr.sbin.mysqld

找到:

# Allow data dir access
  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,

修改为:

# Allow data dir access
  /lvmdata/data/ r,
  /lvmdata/data/** rwk,

重启apparmor服务:

# service apparmor restart

再初始化MySQL:

root@ubuntu:/lvmdata/data# mysqld --initialize --user=mysql --datadir=/lvmdata/data

成功:

root@ubuntu:/lvmdata/data# ls
auto.cnf        ibdata1      ib_logfile1  mysql               sys
ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema

启动服务,登录:

root@ubuntu:/lvmdata/data# service mysql start
root@ubuntu:/lvmdata/data# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

root密码没了。。。

对于Mysql 5.7.6以后的5.7系列版本,Mysql使用mysqld --initialize或mysqld --initialize-insecure命令来初始化数据库,后者可以不生成随机密码。但是安装Mysql时默认使用的是前一个命令,这个命令也会生成一个随机密码。改密码保存在了Mysql的日志文件中。

查找MySQL日志:

root@ubuntu:/var/log/mysql# tail /var/log/mysql/error.log 
2018-01-07T09:43:31.587130Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-01-07T09:43:31.697297Z 0 [ERROR] Too many arguments (first extra is 'mysqld').
2018-01-07T09:43:31.697318Z 0 [ERROR] Aborting

2018-01-07T09:44:51.131747Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-01-07T09:44:51.417055Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-01-07T09:44:51.518291Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-01-07T09:44:51.597737Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 686d9f78-f38f-11e7-a027-000c2927e6cf.
2018-01-07T09:44:51.601893Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-01-07T09:44:51.603154Z 1 [Note] A temporary password is generated for root@localhost: Qs0K5tiIA7-K

最后一行,密码Qs0K5tiIA7-K,再登录成功并要求更改密码:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

更改:

mysql>  SET PASSWORD = PASSWORD('yourpwd');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

圆满解决!

你可能感兴趣的:(Ubuntu初始化MySQL碰到的坑)