1 获取软件
软件url: MySQL :: Download MySQL Community Server (Archived Versions)
2 创建mysql系统账户
# useradd -r -s /sbin/nologin mysql -M
3 解压缩软件
# tar xf mysql-5.7.31-linux-glibc2.12-x86_64
4 移动解压后的目录
# mv mysql-5.7.34-linux-glibc2.12-x86_64 /opt/mysql
5 创建mysql-files目录
# mkdir mysql-files
6 修改mysql-files目录的所有者和所属组
# chown mysql.mysql mysql-files/
7 修改目录的权限
# chmod 750 mysql-files/
drwxr-x---. 2 mysql mysql 6 Aug 29 08:52 mysql-files
8 删除系统自带的my.cnf文件
# rm -f /etc/my.cnf
9 初始化数据库
# bin/mysqld --initialize --user=mysql --basedir=/opt/mysql
2021-08-29T01:20:17.471483Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-29T01:20:17.767881Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-29T01:20:17.858167Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-29T01:20:17.933301Z 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: 4552858a-0867-11ec-a475-000c29aeb505.
2021-08-29T01:20:17.936293Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-29T01:20:18.620351Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-29T01:20:18.666844Z 1 [Note] A temporary password is generated for root@localhost: sDTIKptVe8<D
说明:最后一行出现 root@localhost:*******字样说明初始化数据库成功
10 让数据支持ssl
# bin/mysql_ssl_rsa_setup --datadir=/opt/mysql/data
11 拷贝启动脚本
# cp support-files/mysql.server /etc/init.d/mysqld
12 修改启动脚本basedir和datadir这2个变量
vi /etc/init.d/mysqld 修改46行和47行
46 basedir=/opt/mysql
47 datadir=/opt/mysql/data
13 启动数据库
# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/mysql/data/mysql01.err'. <--错误日志文件
SUCCESS! <--启动成功
14 修改密码为123456
# bin/mysqladmin -uroot password '123456' -p
Enter password: <--这个位置输入的是临时密码sDTIKptVe8<D
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@mysql01 /opt/mysql]#
登录mysql
[root@mysql01 /opt/mysql]# bin/mysql -uroot -p
Enter password: <--新密码123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
15 设置环境变量
echo 'export PATH=/opt/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
16 创建基础配置文件my.cnf
# cat my.cnf
[mysqld]
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/tmp/mysql.sock
17 运行mysql_secure_installation命令
# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
VALIDATE PASSWORD PLUGIN 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 plugin?
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.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success. <--禁用root用户远程登录系统
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!