centos7.2 安装 mysql5.7

一、MySQL 5.7 主要特性:

原生支持 Systemd 

更好的性能:对于多核 CPU、固态硬盘、锁有着更好的优化更好的 InnoDB 存储引擎

更为健壮的复制功能:复制带来了数据完全不丢失的方案,传统金融客户也可以选择使用 MySQL 数据库。

注:mysql-5.6.3 已经支持了多线程的主从复制

新增 sys 库:以后这会是 DBA 访问最频繁的库

二、安装 mysql5.7.18

1、系统环境:centos7.2 x86_64

# uname -r
3.10.0-327.el7.x86_64
# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core)  

因为 centos7.2 默认安装了 mariadb-libs,所以先要卸载掉

查看是否安装 mariadb,有就卸载

# rpm -qa |grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# rpm -e --nodeps mariadb-libs 
# rpm -qa |grep mariadb

2、安装依赖包

注: 相关依赖包的作用

cmake:由于从 MySQL5.5 版本开始弃用了常规的 configure 编译方法,所以需要 CMake 编译器,用于设置 mysql 的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等。 Boost #从 MySQL 5.7.5 开始 Boost 库是必需的,mysql 源码中用到了 C++的 Boost 库,要求必须安装 boost1.59.0 或以上版本

GCC 是 Linux 下的 C 语言编译工具,mysql 源码编译完全由 C 和 C++编写,要求必须安装

GCC

bison:Linux 下 C/C++语法分析器

ncurses:字符终端处理库

软件链接:

安装 cmake,注意进行./bootstrap是报错了,说的是缺少gcc的包

所以需要先安装gcc

# yum -y install gcc gcc-c++ m4
# tar -xvf cmake-3.5.2.tar.gz 
# cd cmake-3.5.2/
# ./bootstrap 
# gmake && gmake install   

cmake –version ---查看 cmake 版本

# cmake -version
cmake version 3.5.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

安装 ncurses

# tar -xvf ncurses-5.9.tar.gz
# cd ncurses-5.9/
# ./configure && make && make install

安装 bison

# tar -xvf bison-3.0.4.tar.gz
# cd bison-3.0.4/
# ./configure && make && make install

安装 bootst

# tar -xvf boost_1_59_0.tar.gz
# mv boost_1_59_0 /usr/local/boost

3)创建 mysql 用户和用户组及目录

# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql  ---新建 msyql 组和 msyql 用户禁止登录 shell
# mkdir /data/soft/mysql                                           ---创建目录
# mkdir /data/soft/mysql/mysqldb                                   ---数据库目录

3、编译安装 mysql

解压 mysql 源码包:

# tar -xvf mysql-5.7.18.tar.gz
# cd mysql-5.7.18/
# cmake -DCMAKE_INSTALL_PREFIX=/data/soft/mysql -DMYSQL_DATADIR=/data/soft/mysql/mysqldb -DSYSCONFDIR=/etc  -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1  -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/data/soft/mysql/mysql.sock -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_c -DWITH-SYSTEMD=1 -DWITH_BOOST=/usr/local/boost
# make && make install  

注 1:配置解释:

-DCMAKE_INSTALL_PREFIX=/data/soft/mysql         	[MySQL  安装的根目录] 
-DMYSQL_DATADIR=/data/soft/mysql/mysqldb    	    [MySQL 数据库文件存放目录]
-DSYSCONFDIR=/etc                                   [MySQL 配置文件所在目录]
-DWITH_MYISAM_STORAGE_ENGINE=1 		                [添加 MYISAM 引擎支持]
-DWITH_INNOBASE_STORAGE_ENGINE=1 	                [添加 InnoDB 引擎支持]
-DWITH_ARCHIVE_STORAGE_ENGINE=1	                    [添加 ARCHIVE 引擎支持 ]
-DMYSQL_UNIX_ADDR=/usr/local/mysql /mysql.sock      [指定 mysql.sock 位置 ]
-DWITH_PARTITION_STORAGE_ENGINE=1	                [安装支持数据库分区 ]
-DEXTRA_CHARSETS=all                                [使 MySQL 支持所有的扩展字符]
-DDEFAULT_CHARSET=utf8                              [设置 MySQL 的默认字符集为utf8] 
-DDEFAULT_COLLATION=utf8_general_ci	                [设置默认字符集校对规则]
-DWITH-SYSTEMD=1	                                [可以使用 systemd 控制 mysql 服务]
-DWITH_BOOST=/usr/local/boost                       [指向 boost 库所在目录]

2:为了加快编译速度可以按下面的方式编译安装

make -j $(grep processor /proc/cpuinfo | wc –l) && make install 

-j 参数表示根据 CPU 核数指定编译时的线程数,可以加快编译速度。默认为 1 个线程编译。 如果在测试时就不要用这个了,比较耗资源,估计两下系统就会显示make错误,我自己做了几次,都是这样提示,所以测试就老老实实make就行,别搞花里胡哨的,在生产环境可以这样用,因为服务器配置比较好。

注 3:若要重新运行 cmake 配置,需要删除 CMakeCache.txt 文件

# make clean
#rm -f  CMakeCache.txt

优化 Mysql 的执行路径

# vim /etc/profile
编辑变量路径内容
export PATH=$PATH:/data/soft/mysql/bin
# source /etc/profile

4、设置权限并初始化 MySQL 系统授权表

# cd /data/soft/mysql/
# chown -R mysql:mysql .
# bin/mysqld --initialize --user=mysql --basedir=/data/soft/mysql --datadir=/data/soft/mysql/mysqldb
2018-05-10T05:20:41.713754Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-05-10T05:20:43.602801Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-05-10T05:20:43.738995Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-05-10T05:20:43.807591Z 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: e337ff8a-5411-11e8-b889-000c29560c16.
2018-05-10T05:20:43.814513Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-05-10T05:20:43.819425Z 1 [Note] A temporary password is generated for root@localhost: _ikVWLuiy0qJ

注意:如果使用–initialize 参数初始化系统数据库之后,会生成 root 用户的一个临时密码"_ikVWLuiy0qJ",如上所示。

注 1:以 root 初始化操作时要加--user=mysql 参数,生成一个随机密码(注意保存登录时用)

注 2:MySQL 5.7.6 之前的版本执行这个脚本初始化系统数据库

/data/soft/mysql/bin/mysql_install_db --user=mysql --basedir=/data/soft/mysql --datadir=/data/soft/mysql/mysqldb

# 5.7.6 之后版本初始系统数据库脚本(本文使用此方式初始化)

#/data/soft/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/soft/mysql --datadir=/data/soft/mysql/mysqldb

# chown  -R  mysql:mysql  .       ---改所有者,注意是 root .

5、创建配置文件

由于在5.7.18开始,二进制包不再包含示例文件my-default.cnf,所以我从5.7.17版本中提取了样例,但是发现里面也没有太多项配置,my-default.cnf内容如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

  自己写的

# vim /etc/my.cnf
[mysqld]
basedir=/data/soft/mysql
datadir=/data/soft/mysql/mysqldb
pid-file=/data/soft/mysql/mysqldb/mysqld.pid
socket=/data/soft/mysql/mysqld.sock
log_error=/data/soft/mysql/mysqldb/mysqld.err

 重新载入 systemd,扫描新的或有变动的单元

# systemctl daemon-reload  

6、配置 mysql 自动启动

# cp /data/soft/mysql/support-files/mysql.server /etc/init.d/mysqld
# chkconfig mysqld on
# systemctl start mysqld
# ps -ef |grep mysqld
root 65697 1 0 14:42 ? 00:00:00 /bin/sh /data/soft/mysql/bin/mysqld_safe --datadir=/data/soft/mysql/mysqldb --pid-file=/data/soft/mysql/mysqldb/mysqld.pid
mysql 65852 65697 6 14:42 ? 00:00:00 /data/soft/mysql/bin/mysqld --basedir=/data/soft/mysql --datadir=/data/soft/mysql/mysqldb --plugin-dir=/data/soft/mysql/lib/plugin --user=mysql --log-error=/data/soft/mysql/mysqldb/mysqld.err --pid-file=/data/soft/mysql/mysqldb/mysqld.pid --socket=/data/soft/mysql/mysql.sock
root 65883 62018 0 14:42 pts/0 00:00:00 grep --color=auto mysqld  

注意:在 mysql.server启动过程中如果出错,那么可能是pid的问题,把默认的 pid 文件指定到了/var/run/mysqld/目录,而并没有事先建立该目录,因此要手动建立该目录并把权限赋给 mysql 用户。

# mkdir /var/run/mysqld
# chown -R mysql:mysql /var/run/mysqld/ 

服务启动成功

访问 MySQL 数据库并修改密码

# mysql -uroot -p_ikVWLuiy0qJ
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 8
Server version: 5.7.18

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> set password = password('abc.123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

使用新密码登陆

# mysql -uroot -pabc.123
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 9
Server version: 5.7.18 Source distribution

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> 

  未完待续................................

原文地址:https://www.cnblogs.com/nshgo/p/9014739.html