Linux安装Mysql5.6

Linux安装Mysql5.6

𝓓𝓸𝓷 Lv6

一、准备工作

1.配置Yum
1
2
3
4
5
6
7
8
[root@mysql opt]# cp -r /run/media/admin/CentOS\ 7\ x86_64/*  /opt/yum

[root@mysql opt]# vi /etc/yum.repos.d/CentOS-Base.repo

[CentOS-Base]
baseurl=file:///opt/yum
gpgcheck=0
enabled=1
2.操作系统依赖包

For MySQL 5.7.19 and later: Support for Non-Uniform Memory Access
(NUMA) has been added to the generic Linux build, which has a dependency
now on the libnuma library; if the library has not been installed on your
system, use you system’s package manager to search for and install it (see
the preceding item for some sample commands).

SLES 11: As of MySQL 5.7.19, the Linux Generic tarball package format is
EL6 instead of EL5. As a side effect, the MySQL client bin/mysql needs
libtinfo.so.5.
A workaround is to create a symlink, such as

ln -s libncurses.so.5.6 /lib64/libtinfo.so.5 on 64-bit systems or

ln-s libncurses.so.5.6 /lib/libtinfo.so.5 on 32-bit systems.

1
2
[root@mysql soft]# yum install libaio
[root@mysql soft]# yum install libnuma
3.二进制安装包

mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

4.安装步骤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files
shell> bin/mysqld --initialize --user=mysql
shell> bin/mysql_ssl_rsa_setup
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
5.Mysql目录结构
Directory Contents of Directory
bin mysqld server, client and utility programs
docs MySQL manual in Info format
man Unix manual pages
include Include (header) files
lib Libraries
share Error messages, dictionary, and SQL for database installation
support-files Miscellaneous support files

二、安装

安装前检查:

[root@mysql mysql]# rpm -qa |grep mysql
[root@mysql mysql]# rpm -qa |grep mariadb

磁盘挂载:

[root@mysql soft]# mkfs.xfs /dev/sdb
meta-data=/dev/sdb isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

[root@mysql soft]# blkid
/dev/sda1: UUID=”28c5912e-d7ae-4c5f-a404-f1806d48ca93” TYPE=”xfs”
/dev/sda2: UUID=”VczXPR-tZhP-TR58-NYzW-3hsR-F3Qc-TzTaO6” TYPE=”LVM2_member”
/dev/sr0: UUID=”2020-11-04-11-36-43-00” LABEL=”CentOS 7 x86_64” TYPE=”iso9660” PTTYPE=”dos”
/dev/sdb: UUID=”804f4903-7d62-4669-b48a-5add1d0d2312” TYPE=”xfs”
/dev/mapper/centos-root: UUID=”6947bf69-372b-4127-b1c9-287c133c0090” TYPE=”xfs”

[root@mysql soft]# vi /etc/fstab

UUID=”804f4903-7d62-4669-b48a-5add1d0d2312” /data xfs defaults 0 0

[root@mysql soft]# mount -a

1.卸载Mariadb
1
2
[root@mysql mysql]# rpm -qa |grep mariadb
[root@mysql mysql]# yum remove mariadb-libs-5.5.68-1.el7.x86_64
2.创建用户和组

groupadd -g 600 mysql

useradd -r -g mysql -u 600 -s /bin/false mysql

温馨提示: -r参数指的是创建系统用户,该用户没有登录权限。

[root@mysql ~]# id mysql
uid=600(mysql) gid=600(mysql) groups=600(mysql)

3.解压安装包

[root@mysql soft]# tar zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

温馨提示:如果tar不支持z选项,则可以使用gunzip命令

gunzip < mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz |tar xvf -

4.创建软件链接

[root@mysql soft]# cd /usr/local/

[root@mysql local]# ln -s mysql-5.7.20-linux-glibc2.12-x86_64 mysql

5.添加PATH环境变量

[root@mysql mysql]# vim /etc/profile

export PATH=/usr/local/mysql/bin:$PATH

温馨提示: 也可以使用sed命令添加

[root@mysql mysql]# sed -i ‘$a export PATH=/usr/local/mysql/bin:$PATH\n’ /etc/profile

[root@mysql mysql]# source /etc/profile

[root@mysql mysql]# tail -2 /etc/profile
export PATH=/usr/local/mysql/bin:$PATH

6.创建数据文件目录

[root@mysql soft]# cd /usr/local/mysql

[root@mysql mysql]# mkdir mysql-files

[root@mysql mysql]# chown mysql:mysql mysql-files
[root@mysql mysql]# chmod 750 mysql-files

7.初如化目录

如果初始化报如下错误:

error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

则安装以下包即可:

yum install libaio-devel

[root@mysql mysql]# mysqld –initialize –user=mysql
2018-01-10T01:53:32.293392Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2018-01-10T01:53:32.718329Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-01-10T01:53:32.786439Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-01-10T01:53:32.859312Z 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: 1039d2a7-f5a9-11e7-be37-0800273f185b.
2018-01-10T01:53:32.860393Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2018-01-10T01:53:32.861180Z 1 [Note] A temporary password is generated for root@localhost: djqWilKg>5t9

–initialize生成一个临时密码:

  • 密码12位数字,4种字符组合

  • 密码有效期180天

  • 如果不想使用这种密码复杂策略,可以使用–initialize-insecure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
初次安装Mysql5.7以上版本后初始root密码找不到的问题
发布时间:2020-08-02 11:07:59 来源:51CTO 阅读:12307 作者:Mr_sheng 栏目:MySQL数据库
mysql5.7新增的特性中主要的一方面就是极大增强了安全性,安装Mysql后默认会为root@localhost用户创建一个随机密码,这个随机密码在不同系统上需要使用不同方式查找,否则无法登录mysql并修改初始密码。

以下以Centos 7为例介绍如何找到初始的随机密码。

在低于Mysql 5.7.6的版本上,Mysql是使用mysql_install_db命令初始化数据库的,该命令会在安装Mysql的用户根目录下创建一个.mysql_secret文件,该文件记录了初始化生成的随机密码,用户可使用改密码登录Mysql并重新修改密码。

对于Mysql 5.7.6以后的5.7系列版本,Mysql使用mysqld --initialize或mysqld --initialize-insecure命令来初始化数据库,后者可以不生成随机密码。但是安装Mysql时默认使用的是前一个命令,这个命令也会生成一个随机密码。改密码保存在了Mysql的日志文件中。

在Centos 7系统上使用rpm命令安装Mysql后,mysql的配置文件是/etc/my.cnf,打开该文件,可以看到mysql的datadir和log文件等的配置信息,如下:

datadir=/var/lib/mysql

log-error=/var/log/mysqld.log

打开/var/log/mysqld.log文件,搜索字符串A temporary password is generated for root@localhost:,可以找到这个随机密码,通常这一行日志在log文件的最初几行,比较容易看到。

使用找到的随机密码登录mysql,首次登录后,mysql要比必须修改默认密码,否则不能执行任何其他数据库操作,这样体现了不断增强的Mysql安全性。
8.配置SSL支持
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@mysql mysql]# mysql_ssl_rsa_setup
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'
-----

分配权限:

chown mysql:mysql *.pem

9.配置my.cnf参数文件

Mysql my.cnf:

cat > /etc/my.cnf <<EOF

[mysqld]

user=mysql

basedir=/application/mysql

datadir=/data/mysql/data

socket=/tmp/mysql.sock

server_id=6

port=3306

[mysql]

socket=/tmp/mysql.sock

EOF

[root@mysql mysql]# cp my.cnf /etc/

10.配置启动文件

两种启动方式:

  • sys-v
1
2
3
4
5
6
cp support-files/mysql.server /etc/init.d/mysqld

/etc/init.d/mysqld start

service mysqld restart

  • systemd
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
38
39
40
41
42
43
44
45
46
47
48
cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
ExecStart= /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
ExecReload= /etc/init.d/mysqld restart
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target

EOF



1,创建启动文件 .service
vim /usr/lib/systemd/system/mysqld.service

[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
Type=forking
PermissionsStartOnly=true
ExecStart= /etc/init.d/mysqld start
ExecStop= /etc/init.d/mysqld stop
ExecReload= /etc/init.d/mysqld restart
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target

2,重新加载一下服务的配置文件
systemctl daemon-reload
3,开启mysql服务
systemctl start mysqld.service
systemctl start mysqld
4,关闭mysql服务
systemctl stop mysqld.service
systemctl stop mysqld

11.Mysql启动

[root@mysql ~]# mysqld_safe –user=mysql &

启动命令 关闭命令
mysqld –user=mysql & mysqladmin -uroot -p shutdown
mysqld_safe –user=mysql & mysqladmin -uroot -p shutdown
/etc/init.d/mysql start /etc/init.d/mysql stop

判断Mysql是否启动:

netstat -lnp |grep 3306

ps -ef |grep mysql

12.创建root密码

[root@mysql ~]# mysqladmin -uroot -p password ‘123456’

13.root密码忘记解决方法

–skip-grant-tables #跳过授权表

–skip-networking #跳过远程登录

1.关闭数据库

2.mysqld_safe –user=mysql –skip-grant-tables –skip-networking &

或者修改/etc/my.cnf增加这两个参数

3.登录数据库update mysql.user表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)


mysql> alter user root@'localhost' identified by '123';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

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

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

4.关闭数据库

5.重启数据库

如果修改的my.cnf,则修改root密码后,一定要将my.cnf去掉参数–skip-grant-tables –skip-networking

14.删除test库相关信息

[root@mysql ~]# mysql_secure_installation

  • Title: Linux安装Mysql5.6
  • Author: 𝓓𝓸𝓷
  • Created at : 2024-08-12 20:18:37
  • Updated at : 2025-01-08 10:13:39
  • Link: https://www.zhangdong.me/mysql5.7-installation.html
  • License: This work is licensed under CC BY-NC-SA 4.0.
评论