关闭

【MySQL学习笔记】CentOS 6.6上二进制方式安装MySQL 5.6

681人阅读 评论(0) 收藏 举报
分类:

二进制安装时基于已安装好的MySQL,

可以参考这个:  【MySQL学习笔记】CentOS 6.6上源码编译方式安装MySQL 5.6

把已安装好的mysql,也就是把/usr/local/mysql 目录直接打包压缩,把压缩文件放到要安装MySQL的机器上,解压之后,MySQL就安装好了。

不过,安装好之后,好需要进行各种配置。


安装、配置过程如下:

一、把mysql程序打包 压缩

[root@wc1 mysql]# cd /usr/local/mysql
[root@wc1 mysql]# tar -zcvf /root/Desktop/mysql-5.6.12.bin.tar.gz

二、把压缩包复制到要安装mysql的机器上解压,然后移动到目标目录

[root@wc1 mysql]# cd /root/Desktop
[root@wc1 Desktop]# tar -zxvf /root/Desktop/mysql-5.6.12.bin.tar.gz 
[root@wc1 Desktop]# mv usr/local/mysql  /usr/local

三:在shell脚本中执行su命令、转换文件格式
由于脚本文件在运行的过程中要切换到mysql用户,也就是命令:su - mysql,导致接下去的命令行都不执行,只有在输入 exit 命令后,才会继续运行,但是运行之后也是有问题的。
然后,搜索解决的办法,从这篇文章中受到启发:http://blog.csdn.net/huashnag/article/details/43967237,感谢作者。

另外,由于shell脚本是在windows下编辑的,linux环境中无法运行,报错:

command not found 3:
command not found 4
command not found 5

QQ群中一个热心的网友说,这是格式问题,需要安装dos2unix,把windows下编辑的文件,转成unix格式的,类似这样:

 dos2unix 文件名

然后,在网上搜索安装方法,安装dos2unix:

[root@wc1 Desktop]# yum -y dos2unix unix2dos
Loaded plugins: fastestmirror, refresh-packagekit, security
No such command: dos2unix. Please use /usr/bin/yum --help
[root@wc1 Desktop]# yum -y install dos2unix unix2dos
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package dos2unix.x86_64 0:3.1-37.el6 will be installed
---> Package unix2dos.x86_64 0:2.2-35.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch             Version               Repository      Size
================================================================================
Installing:
 dos2unix           x86_64           3.1-37.el6            base            16 k
 unix2dos           x86_64           2.2-35.el6            base            14 k

Transaction Summary
================================================================================
Install       2 Package(s)

Total download size: 30 k
Installed size: 35 k
Downloading Packages:
(1/2): dos2unix-3.1-37.el6.x86_64.rpm                    |  16 kB     00:00     
(2/2): unix2dos-2.2-35.el6.x86_64.rpm                    |  14 kB     00:00     
--------------------------------------------------------------------------------
Total                                            76 kB/s |  30 kB     00:00     
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
 Package: centos-release-6-6.el6.centos.12.2.x86_64 (@anaconda-CentOS-201410241409.x86_64/6.6)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : unix2dos-2.2-35.el6.x86_64                                                      1/2 
  Installing : dos2unix-3.1-37.el6.x86_64                                                      2/2 
  Verifying  : dos2unix-3.1-37.el6.x86_64                                                      1/2 
  Verifying  : unix2dos-2.2-35.el6.x86_64                                                      2/2 

Installed:
  dos2unix.x86_64 0:3.1-37.el6                     unix2dos.x86_64 0:2.2-35.el6                    

Complete!
[root@wc1 Desktop]# 

四、脚本

主要有2个脚本文件。

1、mysql_run.sh:在root账号下运行,主要是做一些预备的工作,最后会调用mysql.sh脚本。

2、mysql.sh:在mysql账号下运行。


五、mysql_run.sh

#1.创建用户组、用户、修改目录属主
groupadd mysql

useradd -g mysql mysql

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


#2.修改用户的bash配置文件,在用户登录后,设置语言、执行路径 环境变量
echo "export LANG=zh_CN.UTF-8" >> /home/mysql/.bash_profile
echo "export PATH=/usr/local/mysql/bin:\$PATH" >> /home/mysql/.bash_profile


#3.创建mysql要用的数据目录
mkdir -p /data/mysqldata/{3306/{data,tmp,binlog},backup,scripts} 

chown -R mysql:mysql /data/mysqldata 


#4.设置开机自启动项,里面引用到的脚本是在mysql.sh脚本中创建的
#  通过sed编辑/etc/sudoers文件:
#         1.通过增加 mysql   ALL=(ALL)   ALL,使得mysql账号能使用sudo命令,
#         2.注释 Defaults    requiretty,在终端tty也能执行sudo命令
#
echo "">>  /etc/rc.local 
echo "#autostart mysql">> vi /etc/rc.local
echo "sudo -i -u mysql /data/mysqldata/scripts/mysql_db_startup.sh 3306 > /home/mysql/mysql_db_startup.log 2>&1" >> /etc/rc.local 

sed -i -e '/^root\s*ALL=(ALL)\s*ALL$/a\mysql    ALL=(ALL)    ALL' -e's/^Defaults    requiretty/#Defaults    requiretty/'  /etc/sudoers


#5.在/etc/security/limits.conf文件中增加nproc最大能打开的进程数、nofile最大能打开的文件数 限制
echo "mysql    soft    nproc           2047" >>/etc/security/limits.conf
echo "mysql    hard    nproc           16384" >>/etc/security/limits.conf
echo "mysql    soft    nofile           1024" >>/etc/security/limits.conf
echo "mysql    hard    nofile           65536" >>/etc/security/limits.conf


#6.把mysql.sh文件拷贝到mysql账户的home目录下,设置执行权限
cp mysql.sh /home/mysql/mysql.sh
chmod 777 /home/mysql/mysql.sh


su - mysql -c ./mysql.sh

六:mysql.sh

#7.安装mysl数据库
cd /usr/local/mysql/scripts

./mysql_install_db --datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql


#8.创建my.cnf文件,并输入内容
touch /data/mysqldata/3306/my.cnf 

cat >>/data/mysqldata/3306/my.cnf <<EOF


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


#the mysql server
[mysqld]
port = 3306
user = mysql
socket =/data/mysqldata/3306/mysql.sock
pid-file = /data/mysqldata/3306/mysql.pid


basedir = /usr/local/mysql
datadir = /data/mysqldata/3306/data
tmpdir  = /data/mysqldata/3306/tmp
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


#buffer
max_allowed_packet = 256M
max_heap_table_size = 16M
net_buffer_length = 8K
sort_buffer_size = 1M
join_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 8M


#log
log-bin = /data/mysqldata/3306/binlog/mysql-bin
binlog_cache_size = 32M
max_binlog_cache_size = 512M
max_binlog_size = 512M
binlog_format = mixed
log_output = FILE
log_error = ../mysql-error.log
slow_query_log = 1
slow_query_log_file = ../slow_query.log
expire-logs_days = 14

#innodb
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size = 300M


[mysql]
no-auto_rehash
prompt     =(\u@\h) [\d]>\_
default-character-set = gbk

EOF


#9.创建mysql服务管理脚本,共4个文件
touch /data/mysqldata/scripts/mysql_env.ini

cat >>/data/mysqldata/scripts/mysql_env.ini <<EOF

#set env
MYSQL_USER=''
MYSQL_PASS=''

#check parameter
if [ \$# -ne 1 ]
then
    HOST_PORT=3306
else
    HOST_PORT=\$1
fi

EOF


touch /data/mysqldata/scripts/mysql_db_startup.sh
cat >>/data/mysqldata/scripts/mysql_db_startup.sh <<EOF


#!/bin/sh

source /data/mysqldata/scripts/mysql_env.ini

echo "Startup MySQL Service: localhost_"\${HOST_PORT}

/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/\${HOST_PORT}/my.cnf &

EOF


touch /data/mysqldata/scripts/mysql_db_shutdown.sh
cat >>/data/mysqldata/scripts/mysql_db_shutdown.sh <<EOF


#!/bin/sh

source /data/mysqldata/scripts/mysql_env.ini

echo "Shutdown  MySQL Service: localhost_"\${HOST_PORT}


/usr/local/mysql/bin/mysqladmin -u\${MYSQL_USER} -p\${MYSQL_PASS} -S /data/mysqldata/\${HOST_PORT}/mysql.sock shutdown

EOF


touch /data/mysqldata/scripts/mysqlplus.sh
cat >>/data/mysqldata/scripts/mysqlplus.sh <<EOF

#!/bin/sh

source /data/mysqldata/scripts/mysql_env.ini

echo "Login  MySQL Service: localhost_"${HOST_PORT}

/usr/local/mysql/bin/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sock $2

EOF


chmod 700 /data/mysqldata/scripts/mysql_env.ini
chmod +x /data/mysqldata/scripts/mysql*.sh


#10.把上面管理脚本的路劲,加到msyql用户bash配置文件中,以后就可以直接执行上面的管理脚本,而不需要切换目录了
echo "export PATH=/data/mysqldata/scripts:\$PATH" >> /home/mysql/.bash_profile
source ~/.bash_profile

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:405907次
    • 积分:7197
    • 等级:
    • 排名:第3096名
    • 原创:321篇
    • 转载:12篇
    • 译文:1篇
    • 评论:125条
    博客专栏
    最新评论