数据库
首页 > 数据库> > 在Centos7.7中二进制安装MySQL5.7.27并设置自启动

在Centos7.7中二进制安装MySQL5.7.27并设置自启动

作者:互联网

下载mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz二进制安装包

1.检查操作系统是否自带mysql

[root@CentOS7 ~]# rpm -qa|grep mysql

使用yum命令,因为yum命令可以自动删除与mysql相关的依赖;如果使用rpm命令,则还需要手动去删除和mysql相关的文件

[root@CentOS7 ~]# yum -y remove mysql-libs libaio numactl-libs

检查删除是否成功。如果没有提示则删除成功。

[root@CentOS7 ~]# rpm -qa|grep mysql

2.下载mysql5.7

mysql官网 https://dev.mysql.com/downloads/mysql/5.7.html#downloads
将下载的安装包"mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz"利用xftp上传至/usr/local/app下解压

[root@CentOS7 ~]# [root@CentOS7 ~]# tar xfzv mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/
[root@CentOS7 app]# cd /usr/local/
[root@CentOS7 app]# ln -s mysql57 mysql 
[root@CentOS7 app]# ll
total 0
lrwxrwxrwx.  1 root  root    7 Feb 22 16:56 mysql -> mysql57
drwxr-xr-x. 11 mysql mysql 151 Feb 22 16:24 mysql57
[root@CentOS7 app]# chown -R root.root /usr/local/app/mysql/
[root@CentOS7 app]# ll
total 0
lrwxrwxrwx.  1 root root   7 Feb 22 16:56 mysql -> mysql57
drwxr-xr-x. 11 root root 151 Feb 22 16:24 mysql57

顺手删除mysql5.7的源码包

[root@CentOS7 ~]# rm -f mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz

修改文件名

[root@CentOS7 ~]# mv /usr/local/mysql-5.7.27-linux-glibc2.12-x86_64/ /usr/local/mysql57

3.创建mysql用户组和mysql用户

[root@CentOS7 ~]# cat /etc/group | grep mysql
#查看有没有mysql用户
[root@CentOS7 ~]# cat /etc/passwd |grep mysql
#若没有则创建mysql组
#创建mysql用户组
[root@CentOS7 ~]# groupadd mysql
#创建mysql用户并添加到mysql用户组中(//useradd -r参数表示mysql用户是系统用户,不可用于登录系统;第一个mysql是用户组,第二个mysql是用户)
[root@CentOS7 ~]# useradd -r -g mysql mysql
#检查用户组是否创建成功
[root@CentOS7 ~]# groups mysql
mysql : mysql

进入data创建data文件夹

[root@CentOS7 ~]# mkdir -pv  mysql/data
mkdir: created directory ‘/data’
[root@CentOS7 data]# chown -R mysql:mysql /data/
[root@CentOS7 data]# ll 
total 0
drwxr-xr-x. 3 mysql mysql 18 Feb 22 15:49 mysql

4.设置mysql目录访问权限,用户组

#将mysql目录访问权限赋为myql用户
[root@CentOS7 mysql57]# chown -R mysql /usr/local/mysql57
#改变mysql目录的用户组属于mysql组
[root@CentOS7 mysql57]# chgrp -R mysql /usr/local/mysql57
#查看mysql目录下所有的目录及文件夹所属组合用户
[root@CentOS7 mysql57]# ll
total 36
drwxr-xr-x.  2 mysql mysql  4096 Feb 22 13:15 bin
-rw-r--r--.  1 mysql mysql 17987 Jun 10  2019 COPYING
drwxr-xr-x.  2 mysql mysql    55 Feb 22 13:15 docs
drwxr-xr-x.  3 mysql mysql  4096 Feb 22 13:15 include
drwxr-xr-x.  5 mysql mysql   230 Feb 22 13:15 lib
drwxr-xr-x.  4 mysql mysql    30 Feb 22 13:15 man
-rw-r--r--.  1 mysql mysql  2478 Jun 10  2019 README
drwxr-xr-x. 28 mysql mysql  4096 Feb 22 13:15 share
drwxr-xr-x.  2 mysql mysql    90 Feb 22 13:15 support-files

5.配置mysql

创建以下文件,设置访问权限,用于mysql配置中

第一步:创建文件/tmp/mysql.sock。并设置权限

创建文件

[root@CentOS7 mysql57]# mkdir tmp
[root@CentOS7 mysql57]# cd tmp/
[root@CentOS7 tmp]# ll
total 0
[root@CentOS7 tmp]# touch mysql.sock
[root@CentOS7 tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Feb 22 16:20 mysql.sock

设置权限

[root@CentOS7 tmp]# chown -R mysql:mysql /usr/local/mysql57/tmp/mysql.sock
[root@CentOS7 tmp]# chmod 755 /usr/local/mysql57/tmp/mysql.sock
[root@CentOS7 tmp]# ll
total 0
-rwxr-xr-x. 1 mysql mysql 0 Feb 22 16:20 mysql.sock

第二步:创建/log/mysqld.log。并设置权限

[root@CentOS7 tmp]# cd ..
[root@CentOS7 mysql57]# mkdir log
[root@CentOS7 mysql57]# cd log/
[root@CentOS7 log]# ll
total 0
[root@CentOS7 log]# touch mysqld.log
[root@CentOS7 log]# chown -R mysql:mysql /usr/local/mysql57/log/mysqld.log
[root@CentOS7 log]# chmod 755 /usr/local/mysql57/log/mysqld.log
[root@CentOS7 log]# ll
total 0
-rwxr-xr-x. 1 mysql mysql 0 Feb 22 16:25 mysqld.log

如果出错,说明路径没有写全,要写绝对路径

第三步:创建/tmp/mysqld.pid。并设置权限

[root@CentOS7 log]# cd ../tmp/
[root@CentOS7 tmp]# touch mysql.pid
[root@CentOS7 tmp]# chown -R mysql:mysql /usr/local/mysql57/tmp/mysql.pid 
[root@CentOS7 tmp]# chmod 755 /usr/local/mysql57/tmp/mysql.pid 
[root@CentOS7 tmp]# ll
total 0
-rwxr-xr-x. 1 mysql mysql 0 Feb 22 16:28 mysql.pid
-rwxr-xr-x. 1 mysql mysql 0 Feb 22 16:20 mysql.sock

6.准备配置文件及初始化mysql

[root@CentOS7 mysql57]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql57/ --datadir=/usr/local/mysql57/data/
2020-02-22T09:41:17.147240Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-02-22T09:41:17.388772Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-02-22T09:41:17.434144Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-02-22T09:41:17.498417Z 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: 795ea97d-5557-11ea-8cfa-000c29c92d84.
2020-02-22T09:41:17.499244Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-02-22T09:41:17.500314Z 1 [Note] A temporary password is generated for root@localhost: m!8dl.0g&gjQ

密码:m!8dl.0g&gjQ
配置SSL参数(在mysql目录下)

[root@CentOS7 mysql57]# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql57/data/
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'
-----

由于mysql-5.7.23版本my.cnf不在/support-files下,故我们创建my.cnf文件。

[root@CentOS7 mysql57]# touch /etc/my.cnf
[root@CentOS7 mysql57]# vim /etc/my.cnf 
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql57
datadir=/usr/local/mysql57/data
port = 3306
socket=/tmp/mysql.sock
log-error=/usr/local/mysql57/log/mysqld.log
pid-file=/usr/local/mysql57/tmp/mysqld.pid
#表名不区分大小写
lower_case_table_names = 1
max_connections=5000
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@CentOS7 mysql57]# chown -R mysql:mysql  /usr/local/mysql57/

配置mysql自动启动(可根据需要配置)

[root@localhost mysql57]# cp support-files/mysql.server /etc/init.d/mysql
[root@localhost mysql57]# vim /etc/init.d/mysql

OTg2MjQ3,size_16,color_FFFFFF,t_70)
在这里插入图片描述
若配置了mysql自启动方式则可以使用服务方式启动mysql


#查看mysql状态
#/etc/init.d/mysql status #或者 service mysql
[root@CentOS7 mysql57]# /etc/init.d/mysql status #或者 service mysql status
 ERROR! MySQL is not running
#启动mysql
#/etc/init.d/mysql start #或者 service mysql start
[root@CentOS7 mysql57]# service mysql start
Starting MySQL. SUCCESS! 
#停止mysql
#/etc/init.d/mysql stop 或者 service mysql stop
[root@CentOS7 mysql57]# /etc/init.d/mysql stop
Shutting down MySQL.. SUCCESS! 
#重新启动mysql
#/etc/init.d/mysql restart 或者 service mysql restart
[root@CentOS7 mysql57]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
#查看mysql服务说明启动成功
#ps -ef|grep mysql
[root@CentOS7 mysql57]# ps -ef|grep mysql
root       5414      1  0 18:10 pts/0    00:00:00 /bin/sh /usr/local/mysql57/bin/mysqld_safe --datadir=/usr/local/mysql57/data --pid-file=/usr/local/mysql57/tmp/mysqld.pid
mysql      5665   5414  0 18:10 pts/0    00:00:00 /usr/local/mysql57/bin/mysqld --basedir=/usr/local/mysql57 --datadir=/usr/local/mysql57/data --plugin-dir=/usr/local/mysql57/lib/plugin --user=mysql --log-error=/usr/local/mysql57/log/mysqld.log --pid-file=/usr/local/mysql57/tmp/mysqld.pid --socket=/tmp/mysql.sock --port=3306
root       5695   1262  0 18:10 pts/0    00:00:00 grep --color=auto mysql

启动mysql

#启动mysql
#/etc/init.d/mysql start #或者 service mysql start
[root@CentOS7 mysql57]# service mysql start
Starting MySQL. SUCCESS! 

报错了,上面说没有/usr/local/mysql57/tmp/mysqld.pid。

解决方案:

a)创建文件/usr/local/mysql57/tmp/mysqld.pid

b)修改权限

修改存放mysqld.pid文件目录的权限

chown -R mysql /usr/local/mysql57/tmp
chgrp -R mysql /usr/local/mysql57/tmp
chmod 777 /usr/local/mysql57/tmp

重新启动成功(如果还不行,就是中间某个步骤写错了。或者直接把mysql目录权限赋为777)

[root@CentOS7 mysql57]# service mysql start
Starting MySQL. SUCCESS! 

8.配置mysql到环境变量

[root@CentOS7 mysql57]# vim /etc/profile
export MYSQL57_HOME=/usr/local/mysql57/bin #mysql5.7/bin路径
export PATH=$PATH:${MYSQ57_HOME}

在这里插入图片描述

[root@CentOS7 mysql57]# source /etc/profile
[root@CentOS7 local]# ln -sv mysql57/ mysql
‘mysql’ -> ‘mysql57/’

9.mysql忘记密码

切换到mysql57下的bin目录

[root@CentOS7 bin]# ./mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)

密码错误。。而且之前安装的密码也忘记了
解决方案:

第一步:跳过MySQL的密码认证过程

(注:windows下修改的是my.ini)

[root@CentOS7 bin]# vim /etc/my.cnf

在这里插入图片描述
保存并退出(esc–> :wq)

第二步:重启mysql

[root@CentOS7 bin]# /etc/init.d/mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS!

第三步:登录mysql

进入mysql/bin目录,启动mysql

[root@CentOS7 bin]# ./mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27 MySQL Community Server (GPL)

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.

启动成功

第四步:使用sql语句修改密码

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string=password("mysql") where user="root";
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

注意:mysql5.7没有password字段

ERROR 1054 (42S22): Unknown column ‘password’ in ‘field list’

第五步:重新编辑my.cnf

去掉[mysqld]后面的“skip-grant-tables”

[root@CentOS7 bin]# vim /etc/my.cnf

重启mysql

[root@CentOS7 bin]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

10.设置mysql远程登录
先本地登录mysql

[root@CentOS7 bin]# ./mysql -uroot -pmysql
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.27

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.

mysql> 

10.1报错
突然报错

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决方案:需要重新修改一下密码

mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye
[root@CentOS7 bin]# ./mysql -uroot -pmysql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@CentOS7 bin]# ./mysql -uroot -proot
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 4
Server version: 5.7.27 MySQL Community Server (GPL)

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.

mysql> 

10.2继续配置

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update  user  set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

10.3重启mysql

[root@CentOS7 bin]# service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

10.4设置防火墙
a)配置防火墙开启3306端口

[root@localhost bin]# /sbin/iptables -I INPUT -p tcp --dport 3306-j ACCEPT
[root@localhost bin]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:   [  OK  ]
[root@localhost bin]# /etc/rc.d/init.d/iptables restart
iptables: Setting chains to policy ACCEPT: filter             [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                           [  OK  ]
iptables: Applying firewall rules:                        [  OK  ]

b)临时关闭防火墙

[root@CentOS7 bin]# service iptables stop
Redirecting to /bin/systemctl stop iptables.service
Failed to stop iptables.service: Unit iptables.service not loaded.

c)永久关闭防火墙

[root@CentOS7 bin]# chkconfig iptables off
error reading information on service iptables: No such file or directory

重启后永久生效

10.5远程连接mysql

连接成功
在这里插入图片描述
Mysql 自动启动

1、将服务文件拷贝到init.d下,并重命名为mysql

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

2、赋予可执行权限

chmod +x /etc/init.d/mysqld

3、添加服务

chkconfig --add mysqld

4、显示服务列表

chkconfig --list

如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则键入

chkconfig --level 345 mysqld on

5、重启电脑

reboot

6、验证

netstat -na | grep 3306

如果看到有监听说明服务启动了

或者用ps -ef|grep mysql 或者 service mysqld status

System_Hocker 发布了8 篇原创文章 · 获赞 0 · 访问量 140 私信 关注

标签:27,local,MySQL5.7,CentOS7,usr,mysql,自启动,mysql57,root
来源: https://blog.csdn.net/qq_41986247/article/details/104439210