预备工作:
OS:Centos7.1
DATABASE: mysql-5.6.30.tar.gz
wget http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/mysql-5.6.30.tar.gz
1. 创建mysql帐号
创建用户和用户组
[root@localhost ~]# groupadd mysql [root@localhost ~]# useradd -g mysql mysql [root@localhost ~]# passwd mysql
2.验证安装包
解压mysql源码包mysql-5.6.30.tar.gz
[root@localhost ~]# mkdir -p /opt/mysql-5.6/ [root@localhost ~]# tar zxvf mysql-5.6.30.tar.gz -C /opt/mysql-5.6 [root@localhost ~]# cd /opt/mysql-5.6/
验证之前是否安装过mysql,若存在mysql相关包,则rpm -e逐个删除(建议先删devel包,再mysql包,最后强制删除libs包,centos 7默认为中为mariadb.*,依次删除)
[root@localhost mysql-5.6]# rpm -qa|grep mysql
再验证以下编译必备安装包是否安装:
gcc* gcc-c++* cmake autoconf* automake* zlib* ncurses-devel* libtool* bison
当缺少相关安装包时,需要通过rpm -ivh ***.rpm或yum install RPM_NAME安装
3.源码编译过程
[root@localhost mysql-5.6]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6 #安装目录 -DWITH_INNOBASE_STORAGE_ENGINE=1 #支持InnoDB引擎 -DWITH-READLINE=1 #支持快捷键 -DEXTRA_CHARSETS=all #安装所有字符集 -DENABLED_LOCAL_INFILE=1 #从本地导入数据 -DWITH_EMBEDDED_SERVER=1 #编译Embedded Mysql库 -DWITH_SSL=bundled # -DDEFAULT_CHARSET=utf8 #默认字符集 [root@localhost mysql-5.6]# make [root@localhost mysql-5.6]# make install [root@localhost mysql-5.6]# ln -s /usr/local/mysql-5.6 /usr/local/mysql
4.创建mysql数据目录及/etc/my.cnf
数据存放目录,默认为basedir下的data目录(实际场景下,建议mount专用盘作为data):
[root@localhost mysql-5.6]# cd /usr/local/mysql [root@localhost mysql]# mkdir -p /opt/mysql/data1 [root@localhost mysql]# chown -R mysql:mysql /opt/mysql [root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql [root@localhost mysql]# vi /etc/my.cnf [client] port = 3306 socket =/opt/mysql/data1/mysqld.sock [mysqld_safe] user = mysql nice = 0 [mysqld] port = 3306 pid-file = /opt/mysql/data1/mysqld.pid socket = /opt/mysql/data1/mysqld.sock basedir = /usr/local/mysql datadir = /opt/mysql/data1/ tmpdir = /opt/mysql/data1/ log-error = /opt/mysql/data1/mysqld.log slow_query_log =1 slow_query_log_file = /opt/mysql/data1/mysql-slow.log log_bin = /opt/mysql/data1/mysql-bin.log [mysqldump] quick quote-names max_allowed_packet = 16M default-character-set = utf8 [mysql] default-character-set = utf8
4.初始化mysql
建议进入/usr/local/mysql目录下,通过以下方式进行初始化,否则可能会报错
[root@localhost mysql]# ./scripts/mysql_install_db --defaults-file=/etc/my.cnf [root@localhost mysql]# ./bin/mysql_secure_installation
5.将mysql服务加入init.d管理
建议进入/usr/local/mysql目录下,通过以下方式进行初始化,否则可能会报错
[root@localhost mysql]# cp ./bin/mysql /etc/init.d/mysql [root@localhost mysql]# chkconfig --add mysql [root@localhost mysql]# chkconfig mysql on [root@localhost mysql]# chkconfig --list
同时更改环境变量 vi ~/.bash_profile,在PATH变量后添加:/usr/local/mysql/bin
6.启动mysql服务的几种方式
mysqld与mysqld_safe的区别在于mysqld_safe宕机后仍能重启
[root@localhost mysql]# service mysql start
[root@localhost mysql]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@localhost mysql]# mysqld --defaults-file=/etc/my.cnf &
Mysql多实例安装
多实例的几个好处:mysql多实例共用同一个mysql程序,但是使用不同的配置文件(可以相同)及端口(必须不同)来启动程序,数据文件存放在不同的文件夹中。这些多实例任务相互独立, 可以有效的利用资源,实现资源的逻辑隔离。
上述1-6歩主要安装了使用3306端口的实例,我们同样可以生成其他端口的实例。这里我们通过以下步骤安装3310端口的MySQL实例。
1.创建使用3310端口的mysql数据目录及/etc/my3310.cnf
[root@localhost mysql-5.6]# cd /usr/local/mysql [root@localhost mysql]# mkdir -p /opt/mysql/data2 [root@localhost mysql]# vi /etc/my3010.cnf [client] port = 3310 socket =/opt/mysql/data2/mysqld.sock [mysqld_safe] user = mysql nice = 0 [mysqld] port = 3310 pid-file = /opt/mysql/data2/mysqld.pid socket = /opt/mysql/data2/mysqld.sock basedir = /usr/local/mysql datadir = /opt/mysql/data2/ tmpdir = /opt/mysql/data2/ log-error = /opt/mysql/data2/mysqld.log slow_query_log =1 slow_query_log_file = /opt/mysql/data2/mysql-slow.log log_bin = /opt/mysql/data2/mysql-bin.log [mysqldump] quick quote-names max_allowed_packet = 16M default-character-set = utf8 [mysql] default-character-set = utf8
2.初始化使用3310端口的mysql
建议进入/usr/local/mysql目录下,通过以下方式进行初始化,否则可能会报错
[root@localhost mysql]# ./scripts/mysql_install_db --defaults-file=/etc/my3310.cnf [root@localhost mysql]# ./bin/mysql_secure_installation --defaults-file=/etc/my3310.cnf
3.编辑多实例mysql的配置文件
[root@localhost mysql]# vi /etc/mysql_multi.cnf [mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin [mysqld3306] port = 3306 pid-file = /opt/mysql/data1/mysqld.pid socket = /opt/mysql/data1/mysqld.sock basedir = /usr/local/mysql datadir = /opt/mysql/data1/ tmpdir = /opt/mysql/data1/ log-error = /opt/mysql/data1/mysqld.log slow_query_log =1 slow_query_log_file = /opt/mysql/data1/mysql-slow.log log_bin = /opt/mysql/data1/mysql-bin.log [mysqld3310] [client] port = 3310 socket =/opt/mysql/data2/mysqld.sock [mysqld_safe] user = mysql nice = 0 [mysqld] port = 3310 pid-file = /opt/mysql/data2/mysqld.pid socket = /opt/mysql/data2/mysqld.sock basedir = /usr/local/mysql datadir = /opt/mysql/data2/ tmpdir = /opt/mysql/data2/ log-error = /opt/mysql/data2/mysqld.log slow_query_log =1 slow_query_log_file = /opt/mysql/data2/mysql-slow.log log_bin = /opt/mysql/data2/mysql-bin.log [mysqldump] quick quote-names max_allowed_packet = 16M default-character-set = utf8 [mysql] default-character-set = utf8
4.启动mysql多实例
[root@localhost mysql]# mysqld_multi --defaults-file=/etc/mysql_multi.cnf start 3306 [root@localhost mysql]# mysqld_multi --defaults-file=/etc/mysql_multi.cnf start 3310