MySQL:mysql_install_db 与 –initialize
在过去,我们装好了MySQL的服务后,要创建MySQL的数据库,我们使用:mysql_install_db。
但在最新的版本中,该功能被替换了:
1 2 3 4 |
[root@mysql ~]# mysql_install_db 2016-07-11 23:23:04 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2016-07-11 23:23:04 [ERROR] The data directory needs to be specified. [root@mysql ~]# |
通过上面的命令反馈,你可以看到,MySQL在数据库初始化方面,推荐我们使用:mysqld –initialize。
正式开始操作前,先查看下配置文件中的相关设置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@mysql ~]# cat /etc/my.cnf [mysqld] pid-file=/var/run/mysqld/mysqld.pid log-error=/var/log/mysqld.log datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [root@mysql ~]# |
你需要关注的是:
datadir,这个目录中需要为空,该路径是存放你的MySQL数据文件的地方,你可以自行指定。
log-eror,日志文件,所有MySQL相关的命令输出以及发生的事件,都可以在日志文件中找到对应的条目。
查看下datadir的状态,并置空:
1 2 3 4 5 6 7 8 9 10 11 |
[root@mysql ~]# ls /var/lib/mysql auto.cnf client-key.pem ib_logfile1 mysql.sock public_key.pem xb_doublewrite ca-key.pem ib_buffer_pool ibtmp1 mysql.sock.lock server-cert.pem ca.pem ibdata1 mysql performance_schema server-key.pem client-cert.pem ib_logfile0 mysqld_safe.pid private_key.pem sys [root@mysql ~]# [root@mysql ~]# rm -rf /var/lib/mysql/* [root@mysql ~]# [root@mysql ~]# ls -ltr /var/lib/mysql/ total 0 [root@mysql ~]# |
开启一个全新的会话,监控日志文件的变化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@mysql ~]# tail -f /var/log/mysqld.log 2016-07-12T06:36:10.958466Z 0 [Note] Shutting down plugin 'CSV' 2016-07-12T06:36:10.958479Z 0 [Note] Shutting down plugin 'MyISAM' 2016-07-12T06:36:10.958507Z 0 [Note] Shutting down plugin 'MRG_MYISAM' 2016-07-12T06:36:10.958521Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA' 2016-07-12T06:36:10.958596Z 0 [Note] Shutting down plugin 'sha256_password' 2016-07-12T06:36:10.958606Z 0 [Note] Shutting down plugin 'mysql_native_password' 2016-07-12T06:36:10.959084Z 0 [Note] Shutting down plugin 'binlog' 2016-07-12T06:36:10.960188Z 0 [Note] /usr/sbin/mysqld: Shutdown complete 2016-07-12T06:36:10.982133Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended ----------------------------- Begin ----------------------------- |
开始操作:
1 2 |
[root@mysql ~]# mysqld --initialize [root@mysql ~]# |
查看datadir的变化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@mysql ~]# ls -ltr /var/lib/mysql/ total 110652 -rw-r-----. 1 mysql mysql 50331648 Jul 11 23:45 ib_logfile1 -rw-r-----. 1 mysql mysql 56 Jul 11 23:45 auto.cnf -rw-------. 1 mysql mysql 1680 Jul 11 23:45 ca-key.pem -rw-r--r--. 1 mysql mysql 1079 Jul 11 23:45 ca.pem -rw-------. 1 mysql mysql 1676 Jul 11 23:45 server-key.pem -rw-r--r--. 1 mysql mysql 1087 Jul 11 23:45 server-cert.pem -rw-------. 1 mysql mysql 1676 Jul 11 23:45 client-key.pem -rw-r--r--. 1 mysql mysql 1087 Jul 11 23:45 client-cert.pem -rw-r--r--. 1 mysql mysql 452 Jul 11 23:45 public_key.pem -rw-------. 1 mysql mysql 1680 Jul 11 23:45 private_key.pem drwxr-x---. 2 mysql mysql 4096 Jul 11 23:45 performance_schema drwxr-x---. 2 mysql mysql 4096 Jul 11 23:45 mysql drwxr-x---. 2 mysql mysql 12288 Jul 11 23:45 sys -rw-r-----. 1 mysql mysql 408 Jul 11 23:45 ib_buffer_pool -rw-r-----. 1 mysql mysql 50331648 Jul 11 23:45 ib_logfile0 -rw-r-----. 1 mysql mysql 12582912 Jul 11 23:45 ibdata1 [root@mysql ~]# |
查看日志文件的变化:
1 2 3 4 5 6 7 |
2016-07-12T06:45:15.725428Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-07-12T06:45:17.601297Z 0 [Warning] InnoDB: New log files created, LSN=45790 2016-07-12T06:45:17.786585Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-07-12T06:45:17.853614Z 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: 320ebc4b-47fc-11e6-91d0-000c29bd70ff. 2016-07-12T06:45:17.854889Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-07-12T06:45:18.148690Z 0 [Warning] CA certificate ca.pem is self signed. 2016-07-12T06:45:18.254419Z 1 [Note] A temporary password is generated for root@localhost: J7ry)jcx&FN4 |
注意:
上面的“[Note] A temporary password is generated for”,就是“mysqld –initialize”命令执行过程中,自动为root@localhost 账户设置的临时口令。
我们使用临时口令登录MySQL:
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 |
[root@mysql ~]# ps -ef | grep mysql root 26719 26272 0 23:43 pts/1 00:00:00 tail -f /var/log/mysqld.log root 26750 25315 0 23:48 pts/0 00:00:00 grep mysql [root@mysql ~]# [root@mysql ~]# /etc/init.d/mysql start Starting mysqld: [ OK ] [root@mysql ~]# [root@mysql ~]# /etc/init.d/mysql status mysqld (pid 27016) is running... [root@mysql ~]# [root@mysql ~]# mysql -u root -p'J7ry)jcx&FN4' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.13-6 Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> |
这里,由于我为了演示方便,明文贴出了登录口令,所以会有上面的这条警告:
mysql: [Warning] Using a password on the command line interface can be insecure.
使用临时口令登录数据库后,你首先需要更改口令,否则你什么都做不了:
1 2 3 |
mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> |
修改口令:
1 2 3 4 5 6 7 8 |
mysql> set password for 'root'@'localhost' = password('abcd1234'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> mysql> alter user 'root'@'localhost' PASSWORD expire never; Query OK, 0 rows affected (0.00 sec) mysql> |
然后,你就可以做希望的操作了:
1 2 3 4 5 6 7 8 9 10 11 12 |
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> |
在初始化数据库的时候,其实还有另一个方法:–initialize-insecure。
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 |
[root@mysql ~]# ls -ltr /var/lib/mysql/ total 110652 -rw-r-----. 1 mysql mysql 50331648 Jul 11 23:45 ib_logfile1 -rw-r-----. 1 mysql mysql 56 Jul 11 23:45 auto.cnf -rw-------. 1 mysql mysql 1680 Jul 11 23:45 ca-key.pem -rw-r--r--. 1 mysql mysql 1079 Jul 11 23:45 ca.pem -rw-------. 1 mysql mysql 1676 Jul 11 23:45 server-key.pem -rw-r--r--. 1 mysql mysql 1087 Jul 11 23:45 server-cert.pem -rw-------. 1 mysql mysql 1676 Jul 11 23:45 client-key.pem -rw-r--r--. 1 mysql mysql 1087 Jul 11 23:45 client-cert.pem -rw-r--r--. 1 mysql mysql 452 Jul 11 23:45 public_key.pem -rw-------. 1 mysql mysql 1680 Jul 11 23:45 private_key.pem drwxr-x---. 2 mysql mysql 4096 Jul 11 23:45 performance_schema drwxr-x---. 2 mysql mysql 4096 Jul 11 23:45 mysql drwxr-x---. 2 mysql mysql 12288 Jul 11 23:45 sys -rw-r-----. 1 mysql mysql 346 Jul 11 23:54 ib_buffer_pool -rw-r-----. 1 mysql mysql 50331648 Jul 11 23:54 ib_logfile0 -rw-r-----. 1 mysql mysql 12582912 Jul 11 23:54 ibdata1 [root@mysql ~]# [root@mysql ~]# rm -rf /var/lib/mysql/* [root@mysql ~]# [root@mysql ~]# ls -ltr /var/lib/mysql/ total 0 [root@mysql ~]# [root@mysql ~]# mysqld --initialize-insecure [root@mysql ~]# |
后台日志:
1 2 3 4 5 6 7 |
2016-07-12T06:57:57.045965Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-07-12T06:57:58.214534Z 0 [Warning] InnoDB: New log files created, LSN=45790 2016-07-12T06:57:58.417494Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-07-12T06:57:58.485489Z 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: f76df266-47fd-11e6-b31e-000c29bd70ff. 2016-07-12T06:57:58.487905Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-07-12T06:57:58.753868Z 0 [Warning] CA certificate ca.pem is self signed. 2016-07-12T06:57:58.774901Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. |
通过日志你可以看到:
[Warning] root@localhost is created with an empty password !
这样的方式创建的数据库的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 26 27 28 29 30 31 32 33 34 35 36 37 |
[root@mysql ~]# ps -ef | grep mysql root 26719 26272 0 Jul11 pts/1 00:00:00 tail -f /var/log/mysqld.log root 27164 25315 0 00:00 pts/0 00:00:00 grep mysql [root@mysql ~]# [root@mysql ~]# /etc/init.d/mysql start Starting mysqld: [ OK ] [root@mysql ~]# [root@mysql ~]# netstat -tupln | grep mysql tcp 0 0 :::3306 :::* LISTEN 27430/mysqld [root@mysql ~]# [root@mysql ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.13-6 Percona Server (GPL), Release 6, Revision e3d58bb Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> |
可以看到,确实没有密码就登录了MySQL,并且没有临时口令带来的修改密码的问题。
————————————————
Done。