您的位置:首页 > 数据库 > MySQL

mysql 5.7版本的二进制安装方法

2018-03-15 11:32 736 查看

mysql安装

mysql安装
版本选择

mysql安装方式

安装后的常规操作

系统初始化优化建议

版本选择

目前mysql系的数据库分成三大分支

mariadb

常见版本:5.5,10.0,10.1,10.2

在三者中,mariadb是一个最为支持”开源”理念的数据库,一些比较先进的技术基本都会由mariadb率先支持。例如目前备受关注的spider engine,如果你要使用,只能采用mariadb做平台来进行测试。

mysql-server

常见版本:5.5,5.6,5.7,8.0

目前常见的mysql-community-server都是oracle收购mysqlab公司之后发布的产品,尤其是mysql5.7,在之前基础上做了很大的一些改进,相对而言比较稳定且高效。

percona-server

常见版本:5.5,5.6,5.7

percona-server是mysql的二次开发版本,本身percona公司就对mysql具有很强的理解,在原生基础上提升了高负载下innodb存储引擎的性能,并提供了更多的参数和命令来控制服务器行为、诊断服务器的性能工具等。在5.6版本上提升很显著,但是5.7版本目前优势不是特别大,无法跟进官方版本。

mysql安装方式

目前这里的安装主要以mysql-5.7版本为例。

所需文件下载地址:https://dev.mysql.com/downloads/mysql/

mysql5.7提供的安装方式如下:

安装方式灵活程度难易程度
包管理器(rpm,yum)一键部署
二进制安装中等较为轻松
源码编译最高繁琐复杂
+ 包管理器安装

yum -y install mysql-community-server


二进制安装

1) 创建mysql服务用户
[root@www ~]# groupadd -g 27 mysql
[root@www ~]# useradd -u 27 -g 27 mysql
2) 解压mysql工具包
[root@www ~]# tar -xvf /path/to/mysql-version.tar.gz -C /opt
3) 查看mysql工具包解压后文件情况
[root@www mysql-5.7.20-linux-glibc2.12-x86_64]# ls
bin  COPYING  docs  include  lib  man  README  share  support-files
4) 设定mysql主配置文件
[root@www mysql-5.7.20-linux-glibc2.12-x86_64]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd 
log-error=/var/log/mysql/error.log
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
5) 创建所需目录并修改权限
[root@www ~]# mkdir /var/lib/mysql
[root@www ~]# mkdir /var/log/mysql
[root@www ~]# chown mysql. -R /var/lib/mysql
[root@www ~]# chown mysql. -R /var/log/mysql
6) 初始化数据库
[root@www ~]# /opt/mysql-5.7.20-linux-glibc2.12-x86_64/bin/mysqld --initialize --user=mysql --defaults-file=/etc/my.cnf
7)启动数据库
[root@www ~]# /opt/mysql-5.7.20-linux-glibc2.12-x86_64/bin/mysqld --user=mysql --defaults-file=/etc/my.cnf &
8) 修改数据库初始root密码
[root@www ~]# grep password /var/log/mysql/error.log
[root@www ~]# /opt/mysql-5.7.20-linux-glibc2.12-x86_64/bin/mysqladmin -uroot -p'6qA8M/i7_f2P' -S /var/lib/mysql/mysql.sock password
9)登陆mysql
[root@www ~]# mysql -uroot -p
password:


源码编译(过程略)

安装后的常规操作

0) 简化命令行

[root@www ~]# vim /etc/bashrc
export PATH=$PATH:/opt/mysql-5.7.20-linux-glibc2.12-x86_64/bin # 这样做的好处在于能够随时调取/opt/mysql-version/bin下的指令
[root@www ~] source /etc/bashrc


1) 调整mysql的启动服务脚本(简化启动脚本)

[root@www bin]# cd /opt/mysql-5.7.20-linux-glibc2.12-x86_64/support-files/
[root@www support-files]# cp mysql.server /etc/init.d
[root@www support-files]# cd /etc/init.d/
[root@www init.d]# ls
aegis  agentwatch  functions  jenkins  mysql.server  netconsole  network  README
[root@www init.d]# vim mysql.server
basedir=/opt/mysql-5.7.20-linux-glibc2.12
4000
-x86_64
datadir=/var/lib/mysql
[root@www init.d]# mv mysql.server mysqld
[root@www init.d]# service mysqld start
Starting MySQL. SUCCESS!


2)调整启动脚本参数(变更mysql进程)

[root@www opt]# ps -ef | grep mysql
root      1040     1  0 16:03 pts/0    00:00:00 /bin/sh /opt/mysql/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/www.caroljy.com.pid
mysql     1183  1040  5 16:03 pts/0    00:00:00 /opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/var/lib/mysql --plugin-dir=/opt/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/lib/mysql/www.caroljy.com.pid --socket=/var/lib/mysql/mysql.sock
root      1214   670  0 16:03 pts/0    00:00:00 grep --color=auto mysql
# 可以看到,我们这里起了两个进程,首先是root用户拥有的mysqld_safe进程,其次是mysql用户拥有的mysqld进程
# 在这里我们建议直接采用mysqld进程的方式来运行mysql的服务,避免出现问题后,进程反复被拉起。
[root@www init.d]# sed -i 's/mysqld_safe/mysqld/' mysqld
# 测试能否正常启动
[root@www init.d]# service mysqld start
Starting MySQL. SUCCESS!


3) 简化登陆方式

# 我们目前登陆mysql的方式是这样的:
[root@www init.d]# mysql -uroot -p -S /var/lib/mysql/mysql.sock
Enter password:
# 这样的登陆方式就显得很麻烦,每次都需要重新键入密码等参数。
# 如果想要简化登陆方式也不是不可以。
# 在mysql5.7提供了login-path这样的一个功能
/mysql_config_editor set --login-path='jenny' --user='root' --password --host='localhost' --socket='/var/lib/mysql/mysql.sock'
# 这样做会在当前用户家目录地下生成一个文件
[root@www init.d]# cd ~
[root@www ~]# ls -a
.              .bash_logout   .cache   file      .mylogin.cnf    .pki              .tcshrc
..             .bash_profile  .cshrc   .lesshst  .mysql_history  .pydistutils.cfg  .viminfo
.bash_history  .bashrc        .elinks  .links    .pip            .ssh
[root@www ~]# file .mylogin.cnf
.mylogin.cnf: data
# 在这个文件中就会记录我们相关的登陆信息,并且该文件不能直接被查看到其内容,一方面也保证了我们登陆数据的安全。
# 下次登陆的时候,我们就可以这样做
[root@www ~]# mysql --login-path=jenny
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>


系统初始化优化建议

关闭numa

有条件的配置raid5或者raid10

调整内核参数

[root@srv-ces-asia-db2 ~]# ulimit -a # 可修改/etc/security/limits.conf文件使其永久生效
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 62794
max locked memory       (kbytes, -l) 64 # 根据需求进行调整
max memory size         (kbytes, -m) unlimited # 根据需求进行调整
open files                      (-n) 655360 # 建议设置成655360。 也可以设置的更大。
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 62794
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited


mysql初始化安全加固

delete from mysql.user where root!='root' of host!='localhost'


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 5.7 mysql 安装