参考博客:https://blog.csdn.net/qq_40417278/article/details/88655451
1、解压tar包
cd /softWare/mysql5.7/
tar -xvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
# tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
2、修改文件夹名称(此步骤可以省略)
mv mysql-5.7.25-linux-glibc2.12-x86_64/ mysql-5.7.25
3、创建data文件夹
cd mysql-5.7.25/
mkdir data
4、查看是否有mysql用户,如果提示不存在,则创建mysql用户。
groups mysql
# 此处如果提示不存在myslq用户组,则创建mysql用户组
groupadd mysql
# 创建mysql用户
useradd -r -s /sbin/nologin -g mysql mysql -d /softWare/mysql5.7/mysql-5.7.25
5、给mysql用户赋权限
chown -R mysql ./
chgrp -R mysql ./
chown -R mysql:mysql data
6、配置参数
bin/mysqld --initialize --user=mysql --basedir=/softWare/mysql5.7/mysql-5.7.25 --datadir=/softWare/mysql5.7/mysql-5.7.25/data
注意:此处会生成一个密码,记得要保存好密码
7、生成mysql密钥对
bin/mysql_ssl_rsa_setup --datadir=/softWare/mysql5.7/mysql-5.7.25/data/mysql
结果如下:
Generating a 2048 bit RSA private key
.......+++
..............+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
...........+++
......................................................................................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
...................................+++
..................................+++
writing new private key to 'client-key.pem'
-----
8、复制server文件到指定目录
cd support-files/
cp mysql.server /etc/init.d/mysql
# 注意:此处不要命名成mysql.server,否则后边启动mysql时会报错。
9、更改server文件中安装目录
vi /etc/init.d/mysql
9、此时启动mysql服务可能报错
启动mysql服务指令:/etc/init.d/mysql start
# service mysql start # mysql服务名就是 /etc/init.d/mysql 中的mysql
# service mysql stop # mysql服务名就是 /etc/init.d/mysql 中的mysql
# service mysql status # mysql服务名就是 /etc/init.d/mysql 中的mysql
报错如下,原因是没有mariadb和mariadb.log。
[root@bogon support-files]# /etc/init.d/mysql start
Starting MySQL.2020-02-13T05:45:18.227723Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/var/lib/mysql/bogon.pid).
创建日志目录及日志文件
cd /var/log/
mkdir mariadb
cd mariadb/
touch mariadb.log
10、此时启动mysql服务可能报错:
[root@bogon mariadb]# /etc/init.d/mysql start
Starting MySQL.2020-02-13T05:47:39.907062Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/var/lib/mysql/bogon.pid).
可以将文件替换一个名字:
mv /etc/my.cnf /etc/my.cnf.backup
11、此时启动mysql服务会显示启动成功
[root@bogon mariadb]# /etc/init.d/mysql start
Starting MySQL.Logging to '/softWare/mysql5.7/mysql-5.7.25/data/bogon.err'.
... SUCCESS!
12、创建软连接,启动mysql
# 创建软连接
ln -s /softWare/mysql5.7/mysql-5.7.25/bin/mysql /usr/bin/
# 启动mysql
service mysql start
启动结果如下:
[root@bogon mysql-5.7.25]# ln -s /softWare/mysql5.7/mysql-5.7.25/bin/mysql /usr/bin/
[root@bogon mysql-5.7.25]# service mysql start
Starting MySQL SUCCESS!
13、使用root用户登录mysql,密码就是刚才生成的密码
mysql -hlocalhost -uroot -px5w*MCFFwA52
如果报错:-bash: 未预期的符号 `(' 附近有语法错误
证明密码中有需要转义的字符,只需要在字符前加反斜杠“\”即可,如下:
mysql -hlocalhost -uroot -pg\(qfaMQEr03a
启动结果如下:
[root@bogon mysql-5.7.25]# mysql -hlocalhost -uroot -px5w*MCFFwA52
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 2
Server version: 5.7.25
Copyright (c) 2000, 2019, 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.
14、更改root账户密码,创建远程链接。
此时已经进入mysql语句界面,输入行显示mysql>,注意sql语句使用分号结尾。
set password=password('123456');
grant all privileges on *.* to 'root'@'%' identified by 'root';
flush privileges;
show databases;
执行过程如下:
mysql> set password=password('123456')
-> ;
Query OK, 0 rows affected, 1 warning (0.07 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by 'root';
Query OK, 0 rows affected, 1 warning (0.05 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.11 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
15、退出mysql语句界面
使用 quit 或者 exit 退出mysql。如下所示:
mysql> quit
Bye
[root@bogon mysql-5.7.25]#
16、查看mysql的安装位置(which mysql)
[root@bogon mysql-5.7.25]# which mysql
/usr/bin/mysql
17、设置开机自启动
将myslq的server文件加入到自启动列表
chkconfig --add /etc/init.d/mysql
18、查看自启动列表
chkconfig --list
结果如下:
[root@bogon mysql-5.7.25]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
如果发现mysql,并且3、4、5项都是on的话,证明自启动设置成功。