CentOS6.5下编译安装mysql-5.6.27

时间:2021-03-04 10:24:48
MySQL的安装分为三种:
1、RPM包安装;(MySQL-5.6.27-1.linux_glibc2.5.i386.rpm-bundle.tar)
2、二进制包安装;(mysql-5.6.27-linux-glibc2.5-i686.tar.gz)
3、源码安装。 (mysql-5.6.27.tar.gz)
源码编译安装,可以更灵活的调整参数,并且能够在同一个服务器上部署多个不同版本的MySQL数据库系统。对于生产环境,就需要根据实际情况作选择,对于需要大批量部署数据库服务器的环境,可以在一台机器上源码编译后,打包成RPM格式,而后其他服务器直接使用这个定制化编译过的版本进行快速安装。
MySQL数据库的源码安装包从5.5版本开始,源码编译配置工具换成了CMake。在编译工程中还需要make和gcc两个程序用于源码编译。后两者一般系统都会自带,唯有CMake,如果当前系统配置了yum,那么直接执行yum install cmake即可。如果当前系统既无yum也没有cmake命令,那么就需要先编译安装CMake。

##安装CMake
[root@db tmp]# ll cmake-2.8.8.tar.gz
-rw-r--r--. 1 root root 5691656 12月 11 17:55 cmake-2.8.8.tar.gz
[root@db tmp]# tar zxf cmake-2.8.8.tar.gz
[root@db tmp]# cd cmake-2.8.8
[root@db cmake-2.8.8]# ./configure
---------------------------------------------
CMake 2.8.8, Copyright 2000-2011 Kitware, Inc.
---------------------------------------------
Error when bootstrapping CMake:
Cannot find appropriate C compiler on this system. ##缺少gcc包
Please specify one using environment variable CC.
See cmake_bootstrap.log for compilers attempted.

---------------------------------------------
Log of errors: /tmp/cmake-2.8.8/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------
[root@db tmp]# rpm -qa | grep make ##检查发现make有了
make-3.81-20.el6.i686
[root@db tmp]# rpm -qa | grep gcc ##检查发现gcc缺少
libgcc-4.4.7-4.el6.i686
[root@db tmp]# yum install gcc ##安装
[root@db tmp]# yum install gcc-c++
[root@db tmp]# rpm -qa | grep gcc
gcc-c++-4.1.2-46.el5.i386
gcc-4.1.2-46.el5.i386
libgcc-4.4.7-4.el6.i686
[root@db cmake-2.8.8]# ./configure
[root@db cmake-2.8.8]# gmake && make install
[root@db cmake-2.8.8]# cmake ##安装完成后就多了个cmake命令
cmake version 2.8.8
Usage

cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
......

##添加用户和组
[root@db ~]# groupadd mysql
[root@db ~]# useradd -r -g mysql mysql

##设置用户操作系统资源的限制
[root@db ~]# vi /etc/security/limits.conf
mysql soft nproc 2047
mysql hard nproc 16384
mysql soft nofile 1024
mysql hard nofile 65536

##编译mysql
[root@db tmp]# ll mysql-5.6.27.tar.gz
-rw-r--r-- 1 root root 33327156 Dec 11 06:43 mysql-5.6.27.tar.gz
[root@db tmp]# tar zxf mysql-5.6.27.tar.gz
[root@db tmp]# cd mysql-5.6.27
[root@db tmp]# ##执行cmake命令开始编译
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ ##软件的安装路径
-DDEFAULT_CHARSET=utf8 \ ##MySQL服务的默认字符集
-DDEFAULT_COLLATION=utf8_general_ci \ ##MySQL服务的默认校队规则
-DEXTRA_CHARSETS=all \ ##指定附加的字符集
-DENABLED_LOCAL_INFILE=1 \ ##是否允许客户端本地加载数据到MySQL服务端
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ ##编译INNODB存储引擎
-DWITH_MYISAM_STORAGE_ENGINE=1 \ ##编译MYISAM存储引擎(MyISAM、MEMORY、MERGE、CSV这四种引擎默认就会编译)
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ ##编译BLACKHOLE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \ ##编译ARCHIVE存储引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1 \ ##编译FEDERATED存储引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ ##不编译EXAMPLE存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \ ##支持分区
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ ##编译PERFORMANCE_SCHEMA
-DCOMPILATION_COMMENT='ZSH for mysqltest' \ ##指定编译信息
-DWITH_READLINE=1 \ ##指定输入输出的处理方式(与能否输入中文有关)
-DMYSQL_TCP_PORT=3306 \ ##指定默认端口
-DMYSQL_USER=mysql \ ##指定用户为mysql
-DMYSQL_DATADIR=/data/3306 \ ##指定MySQL数据库数据文件的存储路径(等效于:datadir=/data/3306)
-DSYSCONFDIR=/data/3306 \ ##指定MySQL参数文件my.cnf的默认路径(等效于:--defaults-file=/data/3306/my.cnf)
-DMYSQL_UNIX_ADDR=/data/3306/mysql.sock ##指定套接字文件的存储路径(等效于:socket=/data/3306/mysql.sock)
##cmake命令执行成功的话,最后输出信息类似:
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/mysql-5.6.27
##如果编译过程中出现错误,或者参数变更要重新配置,可以通过rm命令,删除源代码包目录下的CMakeCache.txt文件,而后重新执行cmake命令,或者干脆将mysql源码目录删除,再重新解压并进行编译。
[root@db mysql-5.6.27]# make && make install ##如果前面一步操作没有碰到错误的话,源码编译方式安装mysql就成功了。
[root@db mysql-5.6.27]# chown -R mysql:mysql /usr/local/mysql ##将/usr/local/mysql目录打包压缩,得到的文件就相当于是一套二进制版本的MySQL了。
[root@db mysql-5.6.27]# vi /etc/profile
export PATH=/usr/local/mysql/bin:$PATH

##创建数据目录
[root@db ~]# mkdir -p /data/3306
[root@db ~]# chown -R mysql:mysql /data

##初始化Mysql
[root@db mysql]# cd /usr/local/mysql
[root@db mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306

##修改Mysql主配置文件
[root@db mysql]# cd /usr/local/mysql
[root@db mysql]# cp support-files/my-default.cnf /data/3306/my.cnf
[root@db 3306]# cd /data/3306/
[root@db 3306]# vi my.cnf
[client]
port = 3306
socket = /data/3306/mysql.sock
default-character-set = utf8

[mysqld]
port = 3306
user = mysql
socket = /data/3306/mysql.sock
pid-file = /data/3306/mysql.pid
basedir = /usr/local/mysql
datadir = /data/3306
log-bin = /data/3306/mysql-bin
lower_case_table_names=1
max_connections=1000
character_set_server=utf8
character_set_client=utf8

[mysql]
default-character-set=utf8

##为mysql提供服务脚本
[root@db 3306]# cd /usr/local/mysql
[root@db mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

##启动Mysql
[root@db mysql]# service mysqld start
Starting MySQL.............................................[ OK ]
[root@db mysql]# service mysqld status
MySQL running (28579) [ OK ]
[root@db mysql]# netstat -tulnp | grep mysql
tcp 0 0 :::3306 :::* LISTEN 28579/mysqld

##登录Mysql测试一下
[root@db 3306]# mysql -uroot -p ##第一次登陆密码为空
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.27-log ZSH for mysqltest ##看见编译信息中的“ZSH for mysqltest”了吗?

Copyright (c) 2000, 2015, 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 |
| test |
+--------------------+
4 rows in set (0.00 sec)

mysql> use test
Database changed
mysql> create table t1 (id int ,name varchar(10));
Query OK, 0 rows affected (4.90 sec)

mysql> insert into t1 values(1,'a'),(2,'b'),(3,'c');
Query OK, 3 rows affected (1.29 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c |
+------+------+
3 rows in set (0.00 sec)

##修改密码,删除空用户(密码为空)
mysql> select user,host,password from mysql.user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | db | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | db | |
+------+-----------+----------+
6 rows in set (0.00 sec)

mysql> update mysql.user set password = PASSWORD('123') where user = 'root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | db | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| | localhost | |
| | db | |
+------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)

mysql> drop user ''@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@db;
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | db | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+-----------+-------------------------------------------+
4 rows in set (0.01 sec)

mysql> update mysql.user set host='%' where user='root' and host='localhost';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | db | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)