阅读背景:

Centos7.4安装mysql5.7.21,YUM和二进制tar.gz包方法(阿里云,腾讯云)

来源:互联网 

官网下载tar.gz包安装20180308更新

下载及解压安装包 (centos 7.4 安装 mysql5.7.21)
官方下载地址为:https://dev.mysql.com/downloads/mysql/
Linux - Generic
(mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz)
wget 下载
wget –no-check-certificate https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

我的安装目录是: /opt/mysql-5.7.21

#安装依赖包
#MySQL依赖于libaio 库。如果这个库没有在本地安装,数据目录初始化和后续的服务器启动步骤将会失败。请使用适当的软件包管理器进行安装。例如,在基于Yum的系统上:
shell> yum search libaio
shell> yum install libaio
#注意 SLES 11:从MySQL 5.7.19开始,Linux通用tar包的格式是EL6而不是EL5。以致于MySQL客户端bin / mysql需要libtinfo.so.5。解决方法是创建软链接,例如64位系统上的ln -s libncurses.so.5.6 /lib64/libtinfo.so.5或32 位系统上的ln -s libncurses.so.5.6 /lib/libtinfo.so.5。
#创建一个mysql用户和组(注意 此用户仅用于运行mysql服务,而不是登录,因此使用useradd -r和-s /bin/false命令选项来创建对服务器主机没有登录权限的用户。)
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
#解压到指定目录
shell> tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz -C /opt
shell> cd /opt
shell> mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql-5.7.21
#配置环境变量
echo "export PATH=$PATH:/opt/mysql-5.7.21/bin" >> /etc/profile
source /etc/profile
#配置数据库目录
数据目录:/opt/mysql-5.7.21/data
参数文件my.cnf:/opt/mysql-5.7.21/etc/my.cnf
错误日志log-error:/opt/mysql-5.7.21/log/mysql_error.log
二进制日志log-bin:/opt/mysql-5.7.21/log/mysql_bin.log
慢查询日志slow_query_log_file:/opt/mysql-5.7.21/log/mysql_slow_query.log
套接字socket文件:/opt/mysql-5.7.21/run/mysql.sock
pid文件:/opt/mysql-5.7.21/run/mysql.pid
创建目录:
shell> mkdir -p /opt/mysql-5.7.21/{data,log,etc,run}
shell> chown -R mysql:mysql /opt/mysql-5.7.21
shell> chmod 750 /opt/mysql-5.7.21/{data,log,etc,run}
#配置my.cnf文件
在/opt/mysql-5.7.21/etc/下创建my.cnf文件,加入如下参数,其他参数根据需要配置
shell> touch /opt/mysql-5.7.21/etc/my.cnf
shell> chown mysql:mysql /opt/mysql-5.7.21/etc/my.cnf
#拷贝my.cnf到/etc/下(拷贝后这个文件为root:root)
shell> cp /opt/mysql-5.7.21/etc/my.cnf /etc/

[client]
port = 3306
socket = /opt/mysql-5.7.21/run/mysql.sock

[mysqld]
port = 3306
socket = /opt/mysql-5.7.21/run/mysql.sock
pid_file = /opt/mysql-5.7.21/run/mysql.pid
basedir = /opt/mysql-5.7.21/
datadir = /opt/mysql-5.7.21/data
default_storage_engine = InnoDB
max_allowed_packet = 128M
max_connections = 2048
open_files_limit = 65535

#skip-name-resolve
lower_case_table_names=1

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'


innodb_buffer_pool_size = 128M
innodb_log_file_size = 128M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0


key_buffer_size = 16M

log-error = /opt/mysql-5.7.21/log/mysql_error.log
log-bin = /opt/mysql-5.7.21/log/mysql_bin.log
slow_query_log = 1
slow_query_log_file = /opt/mysql-5.7.21/log/mysql_slow_query.log
long_query_time = 5


tmp_table_size = 16M
max_heap_table_size = 16M
query_cache_type = 0
query_cache_size = 0

server-id=1

#注意 my.cnf 如果加了 skip-name-resolve 后, navicat不能通过 localhost远程连接

#初始化数据库(输出日志到opt/mysql-5.7.21/log/mysql_error.log)
shell> mysqld --initialize --user=mysql --basedir=/opt/mysql-5.7.21 --datadir=/opt/mysql-5.7.21/data

#腾讯云服务器遇到(初始化错误信息)
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
原因:没有安装libnuma.so.1 或者安装时默认安装的是32的,但db2需要的是64位的。
解决办法:1.如果已经安装了libnuma.so.1,先yum remove libnuma.so.1
2.yum -y install numactl.x86_64

#生成一个临时密码,可以在mysql_error.log文件找到
shell> grep 'temporary password' /opt/mysql-5.7.21/log/mysql_error.log

#需要注意关于timestamp的设置--explicit_defaults_for_timestamp

#生成ssl
shell> mysql_ssl_rsa_setup --basedir=/opt/mysql-5.7.21 --datadir=/opt/mysql-5.7.21/data/
shell> chown mysql:mysql /opt/mysql-5.7.21/data/*.pem

#配置服务,使用systemctl管理
shell> cd /usr/lib/systemd/system
shell> touch mysqld.service

文件内容如下

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# systemd service file for MySQL forking server
#

[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

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/opt/mysql-5.7.21/run/mysql.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd

# Start main service
ExecStart=/opt/mysql-5.7.21/bin/mysqld --daemonize --pid-file=/opt/mysql-5.7.21/run/mysql.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 65535

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false
#让systemctl加载配置服务
shell> systemctl daemon-reload
shell> systemctl enable mysqld.service (设置开机启动)
shell> systemctl is-enabled mysqld

#启动MySQL服务
shell> systemctl start mysqld.service

#MySQL用户初始化
重置密码 (用到上面日志文件中生成的初始密码:ibF<RpDX!9wR)
删除匿名用户
关闭root用户的远程登录
删除测试数据库
shell> /opt/mysql-5.7.21/bin/mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:

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: Y

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N

... 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.

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!
#导入时区
shell> mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

#验证安装
shell> mysqladmin version -u root -p

#客户端连接
mysql -u root -p

#创建用户
mysql>CREATE USER 'fswh'@'localhost' IDENTIFIED BY 'Fswh888$';

#创建数据库test_db
mysql> CREATE DATABASE IF NOT EXISTS `fswh_vipsys` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

#用户授权数据库
#只容许本地访问
mysql> GRANT ALL PRIVILEGES ON fswh_vipsys.* TO fswh@'localhost';
mysql> flush privileges;
#开启远程访问,在Linux下为了安全,默认是不允许mysql本机以外的机器访问mysql数据库服务,因此需要重新授权用户。(注意:新阿里云ECS需要在控制台设置安全组开放3306端口)
mysql> GRANT ALL PRIVILEGES ON fswh_vipsys.* TO fswh@'%' IDENTIFIED BY 'Fswh888$';
mysql> flush privileges;

#mysql 修改用户名密码
mysql> use mysql;
mysql> UPDATE user SET password=password("test123") WHERE user='root';
mysql> flush privileges;
mysql> exit;

#mysql服务的启动/重启/停止
systemctl start mysqld.service
systemctl restart mysqld.service
systemctl stop mysqld.service

#设置开机启动
#检查自启动项列表中没有mysqld这个,如果没有就添加mysqld
chkconfig --list mysqld
chkconfig --add mysqld
#设置MySQL在345等级自动启动
chkconfig --level 345 mysqld on
#或用这个命令设置开机启动
chkconfig mysqld on

更新YUM源到阿里云源

#没有wget则安装
yum -y install wget

cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.back

#CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-5.repo

#CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.repo

#CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

yum makecache

YUM安装(下载速度慢)

# 检查是否已经安装mysql
rpm -qa| grep mysql

[root@AY130623132919765ce8Z /]# rpm -qa| grep mysql
mysql-devel-5.1.73-8.el6_8.x86_64
mysql-libs-5.1.73-8.el6_8.x86_64
mysql-5.1.73-8.el6_8.x86_64

# 卸载mysql
rpm -e mysql --nodeps
rpm -e mysql-devel --nodeps
rpm -e mysql-libs --nodeps

# 查看YUM源 mysql的
yum --enablerepo=remi,remi-test list mysql mysql-server

# 配置YUM源 mysql5.7
# MySQL官网中下载YUM源rpm安装包:https://dev.mysql.com/downloads/repo/yum/
Red Hat Enterprise Linux 6 / Oracle Linux 6 (Architecture Independent), RPM Package
(mysql57-community-release-el6-9.noarch.rpm)

# 下载mysql源安装包
wget --no-check-certificate https://dev.mysql.com/get/mysql57-community-release-el6-9.noarch.rpm

# 安装mysql源
yum localinstall mysql57-community-release-el6-9.noarch.rpm

# 检查mysql源是否安装成功
yum repolist enabled | grep mysql

# 可以修改vim /etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。

#安装
sudo yum install mysql-community-server

#启动
service mysqld start
service mysqld status

官网下载RPM包安装

清理残留

检查是否已经安装mysql

rpm -qa| grep mysql

[root@test /]# rpm -qa | grep mysql
mysql-libs-5.1.71-1.el6.x86_64

卸载mysql

rpm -e mysql-libs –nodeps

在电脑中查找有关mysql的文件尽可能删掉,如果有的话
比如/etc/my.cc或者/etc/mysql


分享到: