CentOS6.5环境下MySQL编译安装

时间:2022-08-27 10:25:10

一、环境
操作系统:CentOS6.5 64位
MySQL版本:mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
MySQL下载地址
链接:http://pan.baidu.com/s/1slwOGPJ 密码:zo7i
二、准备
1、将下载的mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz放到/etc/local目录下

[root@hadoop03 local]# pwd
/usr/local
[root@hadoop03 local]# ll mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
-rw-r--r-- 1 root root 311771412 Aug 19 05:10 mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
[root@hadoop03 local]#

2、卸载系统自带的MySQL

[root@hadoop03 ~]# rpm -qa | grep mysql
mysql-libs-5.1.71-1.el6.x86_64
[root@hadoop03 ~]# rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64

3、解压mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz

[root@hadoop03 local]# tar -zxvf mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz

4、重新命令为mysql

[root@hadoop03 local]# mv mysql-5.6.23-linux-glibc2.5-x86_64 mysql

5、创建用户组和用户
创建dba用户组并指定组ID为101

[root@hadoop03 local]# groupadd -g 101 dba

创建mysqladmin用户

[root@hadoop03 local]# useradd -u 520 -g dba -G root -md /usr/local/mysql mysqladmin 
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@hadoop03 local]# id mysqladmin
uid=520(mysqladmin) gid=101(dba) groups=101(dba),0(root)

为用户mysqladmin设置密码

[root@hadoop03 local]# passwd mysqladmin
Changing password for user mysqladmin.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.

切换用户显示-bash-4.1$的解决办法

[root@hadoop03 local]# su - mysqladmin
-bash-4.1$
-bash-4.1$

拷贝mysqladmin的环境变量至/etc/local/mysql目录下

[root@hadoop03 ~]# cp -r /etc/skel/.* /usr/local/mysql

再次切换用户即可显示正常

[root@hadoop03 ~]# su - mysqladmin
[mysqladmin@hadoop03 ~]$

切换用到用户mysqladmin在/usr/local/mysql目录下创建arch文件夹 mkdir arch

将/usr/local/mysql目录下所有文件的所属组和用户设置成mysqladmin

chown -R mysqladmin:dba /usr/local/mysql

6、编辑/etc/my.cnf 使用命令 vim /etc/my.cnf 将以下配置内容拷贝到my.cnf文件中

[client]
port = 3306
socket = /usr/local/mysql/data/mysql.sock

[mysqld]
port = 3306
socket = /usr/local/mysql/data/mysql.sock
explicit_defaults_for_timestamp=true
skip-external-locking
key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
query_cache_size= 32M
max_allowed_packet = 16M
myisam_sort_buffer_size=128M
tmp_table_size=32M

table_open_cache = 512
thread_cache_size = 8
wait_timeout = 86400
interactive_timeout = 86400
max_connections = 600

# Try number of CPU's*2 for thread_concurrency
#thread_concurrency = 32

#isolation level and default engine
default-storage-engine = INNODB
transaction-isolation = READ-COMMITTED

server-id = 1
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/hostname.pid

#open performance schema
log-warnings
sysdate-is-now

binlog_format = MIXED
log_bin_trust_function_creators=1
log-error = /usr/local/mysql/data/hostname.err
log-bin=/usr/local/mysql/arch/mysql-bin
#other logs
#general_log =1
#general_log_file = /usr/local/mysql/data/general_log.err
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/slow_log.err

#for replication slave
#log-slave-updates
#sync_binlog = 1

#for innodb options
innodb_data_home_dir = /usr/local/mysql/data/
innodb_data_file_path = ibdata1:500M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/arch
innodb_log_files_in_group = 2
innodb_log_file_size = 200M

innodb_buffer_pool_size = 2048M
innodb_additional_mem_pool_size = 50M
innodb_log_buffer_size = 16M

innodb_lock_wait_timeout = 100
#innodb_thread_concurrency = 0
innodb_flush_log_at_trx_commit = 1
innodb_locks_unsafe_for_binlog=1

#innodb io features: add for mysql5.5.8
performance_schema
innodb_read_io_threads=4
innodb-write-io-threads=4
innodb-io-capacity=200
#purge threads change default(0) to 1 for purge
innodb_purge_threads=1
innodb_use_native_aio=on

#case-sensitive file names and separate tablespace
innodb_file_per_table = 1
lower_case_table_names=1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[mysqlhotcopy]
interactive-timeout

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

根据自己电脑内存来配置innodb_buffer_pool_size = 2048M
我的虚拟机内存是2G所以此处我设置成1024M
7、将my.cnf的所属组、用户设置成dba和mysqladmin,并设置640权限

[root@hadoop03 ~]# ll /etc/my.cnf
-rw-r--r-- 1 root root 2218 Aug 19 05:44 /etc/my.cnf
[root@hadoop03 ~]# chown mysqladmin:dba /etc/my.cnf
[root@hadoop03 ~]# chmod 640 /etc/my.cnf
[root@hadoop03 ~]# ll /etc/my.cnf
-rw-r----- 1 mysqladmin dba 2218 Aug 19 05:44 /etc/my.cnf

执行以下命令安装(/usr/local/mysql路径下)

scripts/mysql_install_db  --user=mysqladmin --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 

9、切换到root用户,执行以下命令

#将服务文件拷贝到init.d下,并重命名为mysql
[root@sht-sgmhadoopnn-01 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysql
#赋予可执行权限
[root@sht-sgmhadoopnn-01 mysql]# chmod +x /etc/rc.d/init.d/mysql
#删除服务
[root@sht-sgmhadoopnn-01 mysql]# chkconfig --del mysql
#添加服务
[root@sht-sgmhadoopnn-01 mysql]# chkconfig --add mysql
[root@sht-sgmhadoopnn-01 mysql]# chkconfig --level 345 mysql on

10、启动mysql服务

[mysqladmin@hadoop03 ~]$ bin/mysqld_safe &
[1] 3406
[mysqladmin@hadoop03 ~]$ 170819 21:13:09 mysqld_safe Logging to '/usr/local/mysql/data/hostname.err'.
170819 21:13:09 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

查看mysql进程

mysqladmin@hadoop03 ~]$ ps -ef | grep mysqld
520 3406 2646 0 21:13 pts/1 00:00:00 /bin/sh bin/mysqld_safe
520 4049 3406 2 21:13 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/hostname.err --pid-file=/usr/local/mysql/data/hostname.pid --socket=/usr/local/mysql/data/mysql.sock --port=3306
520 4073 2646 0 21:13 pts/1 00:00:00 grep mysqld

查看mysql端口及状态

[mysqladmin@hadoop03 ~]$ netstat -nlp | grep mysql
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 :::3306 :::* LISTEN 4049/mysqld
unix 2 [ ACC ] STREAM LISTENING 28955 4049/mysqld /usr/local/mysql/data/mysql.sock
[mysqladmin@hadoop03 ~]$ service mysql status
MySQL running (4049) [ OK ]

至此MySQL编译安装完成,并启动成功

[mysqladmin@hadoop03 ~]$ mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.23-log MySQL Community Server (GPL)

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>

注:CentOS 6.X 版本都支持以上编译安装步骤