出于学习mysql,需要进行各种实验,有时需要新的数据库,这时就需要进行初始化,进而得到一个类似于新装的环境。

  1. 首先查看mysql是否启动,若启动则将其关闭
[root@fei-7 ~]# systemctl status mysql 
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since Fri 2020-07-17 21:51:50 CST; 1 day 12h ago
     Docs: man:systemd-sysv-generator(8)
  Process: 17093 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
  Process: 17126 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─17139 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/dat...
           └─17621 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr...

Jul 17 21:51:49 fei-7 systemd[1]: Starting LSB: start and stop MySQL...
Jul 17 21:51:50 fei-7 mysqld[17126]: Starting MySQL. SUCCESS!
Jul 17 21:51:50 fei-7 systemd[1]: Started LSB: start and stop MySQL.
[root@fei-7 ~]# 
[root@fei-7 ~]# 
[root@fei-7 ~]# systemctl stop mysql
#一定要将mysql关闭,不然会导致启动失败。
  1. 删除/usr/local/mysql/data下所有内容。
[root@fei-7 mysql]# cd /usr/local/mysql/data/
#如果是rpm安装的mysql,则去/var/lib/mysql/data

[root@fei-7 data]# ls
auto.cnf       binlog.000004  ca-key.pem       fei-7.err       ib_logfile1   performance_schema  server-key.pem
binlog.000001  binlog.000005  ca.pem           ib_buffer_pool  #innodb_temp  private_key.pem     sys
binlog.000002  binlog.000006  client-cert.pem  ibdata1         mysql         public_key.pem      undo_001
binlog.000003  binlog.index   client-key.pem   ib_logfile0     mysql.ibd     server-cert.pem     undo_002


[root@fei-7 data]# cd ..
[root@fei-7 mysql]# rm -rf data/*
[root@fei-7 mysql]# cd data/
[root@fei-7 data]# ls            #确认删除

  1. 执行初始化命令。
#一定去对应目录执行命令
[root@fei-7 mysql]# cd /usr/local/mysql/
[root@fei-7 mysql]# bin/mysqld  --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
#执行成功后,什么都不会出现,因为不是第一次
  1. 此时启动mysql
[root@fei-7 mysql]# systemctl start mysql 
  1. 在/var/log/mysql-error.log 中找到初始密码。
[root@fei-7 mysql]# grep "password" /var/log/mysql-error.log 
2020-07-19T10:22:49.367780+08:00 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: pgM#ny7tl0Ue

#pgM#ny7tl0Ue即为初始化秘密吗
  1. 执行来创建新的密码。
[root@fei-7 mysql]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root:  #此处输入上一步找到的密码。

The existing password for the user account root has expired. Please set a new password.

New password:         #这一步输入新的密码

Re-enter new password:  #确认输入

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: n        #是否要设置验证密码组件
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n  #是否修改root密码,刚刚修改,不用修改。

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
#是否移除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
#不允许root远程登录?
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

#移除册数库?
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
#刷新库?
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

到此,mysql初始化完成。成为一个全新的数据库。

接下来将此台机器进行克隆,使用两台机器进行。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐