一个计算机技术爱好者与学习者

0%

好好学MySQL:CentOS安装多版本MySQL

1. 前言

卓音工作室的服务器中,已经安装了MySQL5.7。但是,confluence和jira不支持MySQL5.7,为了安装这两款软件,必须安装MySQL5.6或更低的版本。

本文中,我们会在已经具备MySQL5.7的机器上,编译安装MySQL5.6,让这两个版本的MySQL同时存在。

参考文档:

2. 编译安装MySQL

2.1. 安装编译工具

1
2
3
4
5
6
7
yum -y install wget gcc-c++ gcc* \
perl ncurses ncurses-devel \
bison cmake make \
openssl openssl-devel curl-devel \
libxml2 libxml2-devel \
libjpeg* libpng* freetype* \
--setopt=protected_multilib=false

2.2. 下载MySQL源码

搜狐镜像:http://mirrors.sohu.com/mysql/MySQL-5.6/
选择下载:mysql-5.6.34.tar.gz

2.3. 准备用户和目录

1、创建一个Mysql用户

1
useradd mysql

2、新建data和log子目录

1
2
mkdir -p /usr/local/mysql{3306,3307}/data            
mkdir -p /usr/local/mysql{3306,3307}/log

3、修改目录的所属者以及所属组权限

1
2
3
4
chown -R mysql:mysql /usr/local/mysql{3306,3307}/data/  
chown -R mysql:mysql /usr/local/mysql{3306,3307}/log/
chmod 750 /usr/local/mysql{3306,3307}/data
chmod 750 /usr/local/mysql{3306,3307}/log

4、创建mysql相关目录并配置权限

1
2
3
4
5
6
7
8
mkdir -p /usr/local/mysql{3306,3307}/etc  
chown -R mysql.mysql /usr/local/mysql{3306,3307}/etc

mkdir -p /var/run/mysqld{3306,3307}
chown -R mysql.mysql /var/run/mysqld{3306,3307}

mkdir -p /var/lib/mysqld{3306,3307}
chown -R mysql.mysql /var/lib/mysqld{3306,3307}

5、创建mysql.sock文件

1
touch /tmp/mysql{3306,3307}.sock

2.4. 编译安装MySQL

1、解压MySQL源码

1
2
tar -zxvf mysql-5.6.34.tar.gz
cd mysql-5.6.34

2、配置编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql3307 \
-DMYSQL_DATADIR=/usr/local/mysql3307/data \
-DSYSCONFDIR=/usr/local/mysql3307/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_TCP_PORT=3307 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_USER=mysql \
-DWITH_DEBUG=0 \
-DWITH_SSL=system

3、编译安装

1
2
make
make install

这一步时间较长,请耐心等待。

3. 配置MySQL

1、拷贝模板,作为MySQL服务的配置文件

1
cp /usr/local/mysql3307/support-files/my-default.cnf /usr/local/mysql3307/etc/my.cnf

2、修改my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/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 = /usr/local/mysql3307
datadir = /usr/local/mysql3307/data
port = 3307
# server_id = .....
socket = /tmp/mysql3307.sock

# Remove leading # to set options mainly useful for reporting servers.

3、初始化mysql数据库。

1
2
3
4
/usr/local/mysql3307/scripts/mysql_install_db \
--basedir=/usr/local/mysql3307 \
--datadir=/usr/local/mysql3307/data \
--user=mysql

4. 配置MySQL开机自启动

参考文档《CentOS7中安装配置Nginx》

5. 启动MySQL

1、添加mysql被service命令管理

1
cp /usr/local/mysql3307/support-files/mysql.server /etc/init.d/mysqld3307

2、启动mysql

1
service mysqld3307 start

3、设置超级管理员密码

1
/usr/local/mysql3307/bin/mysqladmin -u root password voidking -S /tmp/mysql3307.sock

4、登录mysql

1
mysql -u root -p -S /tmp/mysql3307.sock

输入密码voidking,成功登录mysql5.6。

至此,多版本mysql安装配置成功!

6. 远程连接

1、关闭防火墙

1
systemctl stop firewalld.service

2、授权远程访问

1
mysql> grant all privileges on *.* to 'root'@'%' identified by 'voidking' with grant option;

3、远程连接测试
方法一:使用navicat等图形化工具。
方法二:使用mysql命令,例如 mysql -h 192.168.56.101 -P 3307 -u root -pvoidking