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-static
make && make install
cd /application/
ln -s mysql-5.1.73/ mysql
mkdir -p /application/mysql/{data,tmp}
mkdir -p /var/lib/mysql
chown -R mysql:mysql /application/mysql
chown -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.cnf
echo 'export PATH="/application/mysql/bin:/application/mysql/libexec:${PATH}"' >> /etc/profile
source /etc/profile
which mysql mysqld
which mysql_install_db
mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
/etc/init.d/mysqld start
chkconfig mysqld on
netstat -tunlp |grep 3306
lsof -nPi
ln -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* -y
useradd 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=0
make && make install
ln -s /application/mysql-5.5.60 /application/mysql
mkdir -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.cnf
cp -a /etc/my.cnf{,.`date +%F`}
echo 'export PATH=/application/mysql/bin:${PATH}' >> /etc/profile
source /etc/profile
which mysql
cd /application/mysql/scripts
./mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
cp /application/mysql-5.5.60/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/etc/init.d/mysqld start
chkconfig mysqld on
netstat -tunlp |grep 3306
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
ln -s /application/mysql-5.5.60/tmp/mysql.sock /tmp/mysql.sock
ln -s /application/mysql-5.5.60/tmp/mysql.sock /var/lib/mysql/mysql.sock
mysql 5.6.X 系列编译安装
yum install openssl openssl-devel cmake* libaio* ncurses* zlib* libxml* libmcrypt* libtool* -y
useradd 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=0
make && make install
cd /application/
ln -s mysql-5.6.40/ mysql
mkdir -p /application/mysql/tmp
mkdir -p /var/lib/mysql
chown -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/mysqld
echo 'export PATH="/application/mysql/bin:/application/mysql/scripts:${PATH}"' >> /etc/profile
source /etc/profile
which mysql mysql_install_db
mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
/etc/init.d/mysqld start
chkconfig mysqld on
netstat -tunlp |grep mysqld
ln -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* -y
useradd mysql -s /sbin/nologin -M
tar xvf mysql-5.5.60-linux2.6-x86_64.tar.gz
mkdir -p /application/mysql-5.5.60
cp -a mysql-5.5.60-linux2.6-x86_64/* /application/mysql-5.5.60/
cd /application
ln -s mysql-5.5.60/ mysql
cd /application/mysql/scripts
./mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
mv /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_safe
sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/support-files/mysql.server
cp -a /application/mysql/support-files/mysql.server /etc/init.d/mysqld
ll /etc/init.d/mysqld
mkdir -p /application/mysql/tmp
chmod 777 /application/mysql/tmp
/etc/init.d/mysqld start
chkconfig mysqld on
cp -a /etc/my.cnf{,.`date +%F`}
echo 'export PATH=/application/mysql/bin:${PATH}' >> /etc/profile
source /etc/profile
which mysql
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
ln -s /application/mysql/tmp/mysql.sock /var/lib/mysql/mysql.sock
netstat -tunlp|grep mysql
多实例部署:
1、安装依赖
使用二进制方式安装mysql(也可使用编译安装):
下面以5.5.60二进制安装为例
yum install libaio* ncurses* zlib* libxml* libmcrypt* libtool* -y
useradd mysql -s /sbin/nologin -M
tar xf mysql-5.5.60-linux2.6-x86_64.tar.gz
mkdir -p /application/mysql-5.5.60
cp -a mysql-5.5.60-linux2.6-x86_64/* /application/mysql-5.5.60/
cd /application
ln -s mysql-5.5.60/ mysql
mkdir -p /data/{3306,3307}/data
分别在/data/3306和/data/3307目录下导入my.cnf配置文件
注意:2个配置文件中几个参数:
server-id
port
socket
log-error
log-bin
relay-log
relay_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 shutdown
mysql -u root -pchina123 -S /data/3307/mysql.sock shutdown
chown 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/profile
cd /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.sock
msyql –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* -y
useradd 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=0
make && make install
cd /application/
ln -s mysql-5.6.40/ mysql
mkdir -p /application/mysql/tmp
mkdir -p /var/lib/mysql
chown -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 start
netstat -tunlp|grep mysql
echo "/data/3306/mysqld start " >> /etc/rc.local
echo "/data/3307/mysqld start " >> /etc/rc.local