Mysql的5种安装方法:
1、yum/rpm安装(适合要求不高,并发不大,公司内部,企业内部一些应用场景)2、编译安装(在线高并发业务)3、二进制安装(编译安装后,使用自带工具打包,然后拷贝到相同平台进行安装,在线高并发)4、定制rpm包,yum安装
mysql yum 安装各个版本yum源
http://repo.mysql.com/yum/
mysql 4.X和5.1.X 系列编译安装useradd mysql -s /sbin/nologin -M
./configure \
--prefix=/application/mysql-5.1.73 \--with-unix-socket-path=/application/mysql-5.1.73/tmp/mysql.sock \--localstatedir=/application/mysql-5.1.73/data \--enable-assembler \--enable-thread-safe-client \--with-mysqld-user=mysql \--with-big-tables \--without-debug \--with-pthread \--enable-assembler \--with-extra-chasets=complex \--with-readline \--with-ssl \--with-embedded-server \--enable-local-infile \--with-plugins=partition,innobase \--with-myslqd-ldflags=-all-static \--with-client-ldflags=-all-staticmake && make install
cd /application/
ln -s mysql-5.1.73/ mysqlmkdir -p /application/mysql/{data,tmp}mkdir -p /var/lib/mysqlchown -R mysql:mysql /application/mysqlchown -R mysql:mysql /var/lib/mysql/cp -a /application/mysql/share/mysql/mysql.server /etc/init/mysqld
[ -f /etc/my.cnf ]&&mv /etc/my.cnf{,.ori}cp -a /application/mysql/share/mysql/my-small.cnf /etc/my.cnfecho 'export PATH="/application/mysql/bin:/application/mysql/libexec:${PATH}"' >> /etc/profilesource /etc/profilewhich mysql mysqld which mysql_install_dbmysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
/etc/init.d/mysqld startchkconfig mysqld onnetstat -tunlp |grep 3306lsof -nPiln -s /application/mysql/tmp/mysql.sock /var/lib/mysql/mysql.sock
ln -s /application/mysql/tmp/mysql.sock /tmp/mysql.sock
mysql 5.5 及以上版本依赖安装
yum install openssl openssl-devel cmake* libaio* ncurses* zlib* libxml* libmcrypt* libtool* -yuseradd mysql -s /sbin/nologin -M mysql 5.5.X 系列编译cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.60 \-DMYSQL_DATADIR=/application/mysql-5.5.60/data \-DMYSQL_UNIX_ADDR=/application/mysql-5.5.60/tmp/mysql.sock \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DENABLED_LOCAL_INFILE=ON \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_FEDERATED_STORAGE_ENGINE=1 \-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \-DWITH_FAST_MUTEXES=1 \-DWITH_ZLIB=bundled \-DENABLED_LOCAL_INFILE=1 \-DWITH_READLINE=1 \-DWITH_EMBEDDED_SERVER=1 \-DWITH_DEBUG=0make && make install
ln -s /application/mysql-5.5.60 /application/mysqlmkdir -p /application/mysql/{data,tmp}chown -R mysql:mysql /application/mysql/[ -f /etc/my.cnf ]&&mv /etc/my.cnf{,.ori}
cp -a /application/mysql-5.5.60/support-files/my-small.cnf /etc/my.cnfcp -a /etc/my.cnf{,.`date +%F`}echo 'export PATH=/application/mysql/bin:${PATH}' >> /etc/profilesource /etc/profilewhich mysql cd /application/mysql/scripts./mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysqlcp /application/mysql-5.5.60/support-files/mysql.server /etc/init.d/mysqldchmod +x /etc/init.d/mysqld/etc/init.d/mysqld startchkconfig mysqld onnetstat -tunlp |grep 3306 mkdir -p /var/lib/mysqlchown -R mysql:mysql /var/lib/mysqlln -s /application/mysql-5.5.60/tmp/mysql.sock /tmp/mysql.sockln -s /application/mysql-5.5.60/tmp/mysql.sock /var/lib/mysql/mysql.sockmysql 5.6.X 系列编译安装yum install openssl openssl-devel cmake* libaio* ncurses* zlib* libxml* libmcrypt* libtool* -yuseradd mysql -s /sbin/nologin -M
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.40 \
-DMYSQL_DATADIR=/application/mysql-5.6.40/data \-DMYSQL_UNIX_ADDR=/application/mysql-5.6.40/tmp/mysql.sock \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DENABLED_LOCAL_INFILE=ON \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_FEDERATED_STORAGE_ENGINE=1 \-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \-DWITHOUT_PARTITION_STORAGE_ENGINE=0 \-DWITH_FAST_MUTEXES=1 \-DWITH_ZLIB=bundled \-DENABLED_LOCAL_INFILE=1 \-DWITH_READLINE=1 \-DWITH_EMBEDDED_SERVER=1 \-DWITH_DEBUG=0make && make install
cd /application/
ln -s mysql-5.6.40/ mysqlmkdir -p /application/mysql/tmpmkdir -p /var/lib/mysqlchown -R mysql:mysql /application/mysql/chown -R mysql:mysql /var/lib/mysql/[ -f /etc/my.cnf ]&&mv /etc/my.cnf{,.ori}cp -a /application/mysql/support-files/my-default.cnf /etc/my.cnf #改配置文件需要手动修改,不像5.5系列,这一步最好是上传事先已经准备好的配置文件cp -a /application/mysql/support-files/mysql.server /etc/init.d/mysqldecho 'export PATH="/application/mysql/bin:/application/mysql/scripts:${PATH}"' >> /etc/profile
source /etc/profilewhich mysql mysql_install_dbmysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
/etc/init.d/mysqld startchkconfig mysqld onnetstat -tunlp |grep mysqldln -s /application/mysql/tmp/mysql.sock /var/lib/mysql/mysql.sock
ln -s /application/mysql/tmp/mysql.sock /tmp/mysql.sock
二进制安装:安装包名称中含有linux关键字,所谓二进制安装就是,直接解压后就能使用的安装包
下面以5.5.60二进制安装为例
yum install libaio* ncurses* zlib* libxml* libmcrypt* libtool* -yuseradd mysql -s /sbin/nologin -Mtar xvf mysql-5.5.60-linux2.6-x86_64.tar.gz
mkdir -p /application/mysql-5.5.60cp -a mysql-5.5.60-linux2.6-x86_64/* /application/mysql-5.5.60/cd /applicationln -s mysql-5.5.60/ mysql cd /application/mysql/scripts./mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysqlmv /etc/my.cnf{,.ori}
cp -a /application/mysql/support-files/my-small.cnf /etc/my.cnf
sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safesed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/support-files/mysql.servercp -a /application/mysql/support-files/mysql.server /etc/init.d/mysqldll /etc/init.d/mysqld
mkdir -p /application/mysql/tmp
chmod 777 /application/mysql/tmp /etc/init.d/mysqld startchkconfig mysqld oncp -a /etc/my.cnf{,.`date +%F`}
echo 'export PATH=/application/mysql/bin:${PATH}' >> /etc/profilesource /etc/profilewhich mysqlmkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysqlln -s /application/mysql/tmp/mysql.sock /var/lib/mysql/mysql.socknetstat -tunlp|grep mysql
多实例部署:1、安装依赖
使用二进制方式安装mysql(也可使用编译安装):
下面以5.5.60二进制安装为例yum install libaio* ncurses* zlib* libxml* libmcrypt* libtool* -yuseradd mysql -s /sbin/nologin -Mtar xf mysql-5.5.60-linux2.6-x86_64.tar.gz
mkdir -p /application/mysql-5.5.60cp -a mysql-5.5.60-linux2.6-x86_64/* /application/mysql-5.5.60/cd /applicationln -s mysql-5.5.60/ mysql mkdir -p /data/{3306,3307}/data分别在/data/3306和/data/3307目录下导入my.cnf配置文件
注意:2个配置文件中几个参数:server-idportsocketlog-errorlog-binrelay-logrelay_log_info_file slow_query_log_file不能相同,其中server-id和port一定不能相同注意:必须在/data/{3306,3307}/my.cnf配置文件中指定数据库目录为datadir=/data/{3306,3307}/data,源码包中的配置文件没有这一条指令另外在/data/3306和/data/3307目录下导入事先写好的启动服务的脚本:相关脚本见单独的脚本文件
多实例启动文件的启动mysql服务本质:
mysqld_safe --default-file = /data/3306/my.cnf &> /dev/null &mysqld_safe --default-file = /data/3307/my.cnf &> /dev/null &多实例启动文件的停止mysql服务本质:
mysql -u root -pchina123 -S /data/3306/mysql.sock shutdownmysql -u root -pchina123 -S /data/3307/mysql.sock shutdownchown mysql:mysql -R /data/
sed -i ‘s#/usr/local/mysql#/application/mysql#g’ /application/mysql/bin/mysqld_safe #使用的是官方的二进制包,如果是自己事先编译并打包的二进制包,根据编译时的指定也可以不修改
export ‘export PATH=/application/mysql/bin:${PATH}’ >> /etc/profile
source /etc/profilecd /application/mysql/scripts/ #这里注意因为是多实例,因此需要进行多次初始化
./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql 使用前面的启动脚本分别启动两个实例登录数据库进行测试:
msyql –uroot –pchina123 -S /data/3306/mysql.sockmsyql –uroot –pchina123 -S /data/3307/mysql.sock 注意:Mysql多实例本地管理时,必须加上-S选项指定sock文件编译安装实现多实例:以5.6.40为例说明yum install openssl openssl-devel cmake* libaio* ncurses* zlib* libxml* libmcrypt* libtool* -yuseradd mysql -s /sbin/nologin -M
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.40 \
-DMYSQL_DATADIR=/application/mysql-5.6.40/data \-DMYSQL_UNIX_ADDR=/application/mysql-5.6.40/tmp/mysql.sock \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DENABLED_LOCAL_INFILE=ON \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_FEDERATED_STORAGE_ENGINE=1 \-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \-DWITHOUT_PARTITION_STORAGE_ENGINE=0 \-DWITH_FAST_MUTEXES=1 \-DWITH_ZLIB=bundled \-DENABLED_LOCAL_INFILE=1 \-DWITH_READLINE=1 \-DWITH_EMBEDDED_SERVER=1 \-DWITH_DEBUG=0make && make install
cd /application/
ln -s mysql-5.6.40/ mysqlmkdir -p /application/mysql/tmpmkdir -p /var/lib/mysqlchown -R mysql:mysql /application/mysql/chown -R mysql:mysql /var/lib/mysql/mkdir -p /data/{3306,3307}/data
上传3306实例的配置文件my.cnf和启停脚本mysqld到/data/3306/目录下上传3307实例的配置文件my.cnf和启停脚本mysqld到/data/3307/目录下注意:如果使用源码包中的默认配置文件my.cnf中需要在配置文件中添加一条指令,datadir=/data/{3306,3307}/data指定数据库位置,另外配置文件中端口等配置参数需要根据不同的实例进行相应的修改 启动服务:/data/{3306,3307}/mysqld startnetstat -tunlp|grep mysql
echo "/data/3306/mysqld start " >> /etc/rc.local
echo "/data/3307/mysqld start " >> /etc/rc.local