MySQL5.7单机版安装

准备环境

关闭firewalld

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service

关闭NetworkManager

[root@localhost ~]# systemctl stop NetworkManager.service
[root@localhost ~]# systemctl disable NetworkManager.service

关闭selinux

[root@localhost ~]# vi /etc/selinux/config

修改为以下内容:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

配置IP地址

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

修改为以下内容:

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=no
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.11
NETMASK=255.255.255.0
GATEWAY=192.168.1.2

修改DNS解析

[root@localhost ~]# vi /etc/resolv.conf

修改为以下内容:

nameserver 192.168.1.2

安装基础工具

[root@localhost ~]# yum install -y net-tools vim lrzsz tree screen wget

开始安装

创建mysql用户

  • 创建mysql用户组
  • 创建mysql用户,并加入mysql用户组
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r -g mysql mysql

上传MySQL安装包

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

或者上传本地已有安装包
[root@localhost src]# rz

或者上传本地已有安装包mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz:

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# rz

解压安装包

[root@localhost src]# ls
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar -zxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz 
[root@localhost src]# mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/
[root@localhost src]# ln -s /usr/local/mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost src]# cd 
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql-5.7.20-linux-glibc2.12-x86_64

创建初始化文件夹

[root@localhost ~]# mkdir -p /usr/local/mysql/run
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql/run
[root@localhost ~]# mkdir -p /usr/local/mysql/log
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql/log
[root@localhost ~]# echo "" > /usr/local/mysql/log/mysqld.log
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql/log/mysqld.log

配置/etc/my.cnf

[root@localhost ~]# cp /etc/my.cnf my.cnf.bak
[root@localhost ~]# vim /etc/my.cnf

修改为以下内容:

[client]  
port=3306 
socket = /usr/local/mysql/run/mysqld.sock

[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /usr/local/mysql/run/mysqld.sock
character-set-server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 添加skip-grant-tables,则可以实现无密钥验证
# skip-grant-tables

[mysql]
no-auto-rehash
socket = /usr/local/mysql/run/mysqld.sock

[mysqld_safe]
log-error=/usr/local/mysql/log/mysqld.log
pid-file=/usr/local/mysql/run/mysqld.pid

注意:5.7.18以上版本在没有my-default.cnf

[root@localhost mysql]# cd support-files/
[root@localhost support-files]# pwd
/usr/local/mysql/support-files
[root@localhost support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server

配置环境变量

[root@localhost ~]# vim /etc/profile

追加以下内容:

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

使配置文件生效:

[root@localhost ~]# source /etc/profile

配置service服务

[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# vim /etc/init.d/mysqld

修改basedir和datadir参数值:

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

初始化数据库

[root@localhost ~]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2017-11-26T05:21:53.374950Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-11-26T05:21:53.375022Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2017-11-26T05:21:53.375027Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2017-11-26T05:21:53.807519Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-11-26T05:21:53.869441Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-11-26T05:21:53.936198Z 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: b6dc0859-d269-11e7-b427-000c298afdc8.
2017-11-26T05:21:53.937310Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-11-26T05:21:53.954724Z 1 [Note] A temporary password is generated for root@localhost: i#yed,qj,2(A

手动启动MySQL

[root@localhost bin]# ./mysqld_safe --user=mysql &

检查启动情况,启动成功如下:

[root@localhost bin]# ps -ef|grep mysql
root      11551    990  0 13:23 pts/0    00:00:00 /bin/sh ./mysqld_safe --user=mysql
mysql     11702  11551  1 13:23 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
root      11732    990  0 13:23 pts/0    00:00:00 grep --color=auto mysql

登录客户端

mysql第一次root用户登录密码为初始化时生成的密码,本次为 i#yed,qj,2(A
登录后,重新设置root用户密码

[root@localhost bin]# ./mysql -uroot -p 
Enter password: 
mysql> set password=password('新密码');
mysql> exit;
[root@localhost bin]#

忘记密码时,先停止mysql,再以skip-grant-tables启动:
停止mysql:

[root@localhost bin]# ps -ef|grep mysql
root      11743  10345  0 16:17 pts/1    00:00:00 /bin/sh ./mysqld_safe --user=mysql --skip-grant-tables
mysql     11936  11743  0 16:17 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --skip-grant-tables --log-error=/usr/local/mysql/log/mysqld.log --pid-file=/usr/local/mysql/run/mysqld.pid --socket=/usr/local/mysql/run/mysqld.sock --port=3306
root      12132  10345  0 16:25 pts/1    00:00:00 grep --color=auto mysql
[root@localhost bin]# kill -9 11743
[root@localhost bin]# kill -9 11936
[1]+  已杀死               ./mysqld_safe --user=mysql --skip-grant-tables

安全模式下,启动mysql,并修改mysql的root用户密码:

[root@localhost bin]# ./mysqld_safe --user=mysql --skip-grant-tables &
[root@localhost bin]# ./mysql -u root -p
Enter password: 
mysql> update mysql.user set authentication_string=password('新密码') where user='root'and Host = 'localhost';
mysql> exit;
[root@localhost bin]# 

配置远程访问

创建mysql用户,并开放远程访问权限:

[root@localhost bin]# ./mysql -uroot -p 
Enter password: 
mysql> grant all privileges on *.* to '用户名'@'%' identified by '用户密码' with grant option;
mysql> flush privileges;
mysql> exit;
[root@localhost bin]# 

注意:如果没有关闭防火墙,则需要打开3306端口

设置开机启动

[root@localhost bin]# chkconfig --add mysqld
[root@localhost bin]# chkconfig mysqld on
[root@localhost bin]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld          0:1:2:3:4:5:6:关
netconsole      0:1:2:3:4:5:6:关
network         0:1:2:3:4:5:6:

检查mysql启动/停止服务命令:

[root@localhost bin]# systemctl start mysqld
[root@localhost bin]# systemctl status mysqld
[root@localhost bin]# mysql -h 192.168.56.11 -u mysql -p
Enter password: 
mysql> exit;
[root@localhost bin]# reboot

检查开机启动

[root@localhost ~]# systemctl status mysqld
[root@localhost bin]# mysql -h 192.168.56.11 -u mysql -p
Enter password: 
mysql> exit;

猜你喜欢

转载自blog.csdn.net/zjfjifei2008/article/details/78636045
今日推荐