一.二进制安装MySql
1. 下载Mysql安装包
wget https://downloads.mysql.com/archives/get/file/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
如果没有wget 可以先下载weget
2. 下载初始化依赖
yum install libaio-devel autoconf -y
3. 解压压缩包
tar xf /root/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
4.移动解压后sql文件到/usr/local/目录下
mv /root/mysql-5.6.40-linux-glibc2.12-x86_64 /usr/local/mysql-5.6.40
5.建立软连接
ln -s /usr/local/mysql-5.6.40 /usr/local/mysql
6.拷贝启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
7.覆盖配置文件
rm -f /etc/my.cnf && cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
8.创建mysql用户
useradd mysql -s /sbin/nologin -M
9.初始化
/usr/local/mysql-5.6.40/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
10 授权
chown -R mysql.mysql /usr/local/mysql*
11.添加环境变量
echo 'export PATH="/usr/local/mysql/bin:$PATH"' > /etc/profile.d/mysql.sh
12.加载环境变量
source /etc/profile
13.启动MySql
/etc/init.d/mysqld start
二.源码安装mysql
1)安装依赖
[root@elk02 ~]# yum install -y ncurses-devel libaio-devel cmake
2)解压
[root@elk02 ~]# tar xf mysql-5.6.40.tar.gz
3)创建MySQL用户
[root@elk02 ~]# useradd mysql -s /sbin/nologin -M
4)生成编译文件
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.40 \
-DMYSQL_DATADIR=/usr/local/mysql-5.6.40/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.6.40/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0
5)编译
make
6)安装
make install
7)做软连接
[root@elk02 mysql-5.6.40]# ln -s /usr/local/mysql-5.6.40 /usr/local/mysql
8)拷贝配置文件
[root@elk02 support-files]# cp my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
9)拷贝启动脚本
[root@elk02 support-files]# cp mysql.server /etc/init.d/mysqld
10)初始化
[root@elk02 scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
11)创建socket存放目录
[root@elk02 support-files]# mkdir /usr/local/mysql-5.6.40/tmp
12)授权
[root@elk02 support-files]# chown -R mysql.mysql /usr/local/mysql*
13)添加环境变量
[root@elk02 support-files]# vim /etc/profile.d/mysql.sh
export PATH="/usr/local/mysql/bin:$PATH"
14)加载环境变量
[root@elk02 support-files]# source /etc/profile
15)启动MySQL
[root@elk02 support-files]# /etc/init.d/mysqld start
三.多实例安装mysql
什么是MySQL多实例?
多进程+多线程+多个预先分配内存结构
简言之,在一个系统中安装多个mysql服务端.
需要多个配置文件:
1)多个端口
2)多个数据目录
3)多个socket文件
./mysql_install_db --user --basedir --datadir
1)创建多个数据目录
[root@elk01 ~]# mkdir -p /data/330{7..9}
2)创建多个配置文件
vim /data/3307/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data/3307/data
port = 3307
server_id = 7
log-bin = /data/3307/data/mysql-bin
log_error = /data/3307/data/mysql.err
socket = /data/3307/data/mysql.sock
vim /data/3308/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data/3308/data
port = 3308
server_id = 8
log-bin = /data/3308/data/mysql-bin
log_error = /data/3308/data/mysql.err
socket = /data/3308/data/mysql.sock
vim /data/3309/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data/3309/data
port = 3309
server_id = 9
log-bin = /data/3309/data/mysql-bin
log_error = /data/3309/data/mysql.err
socket = /data/3309/data/mysql.sock
3)初始化
[root@elk01 scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data
[root@elk01 scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/3308/data
[root@elk01 scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/3309/data
4)启动MySQL
[root@elk01 scripts]# mysqld_safe --defaults-file=/data/3307/my.cnf &
[root@elk01 scripts]# mysqld_safe --defaults-file=/data/3308/my.cnf &
[root@elk01 scripts]# mysqld_safe --defaults-file=/data/3309/my.cnf &
5)设置密码
[root@elk01 scripts]# mysqladmin -uroot -S /data/3307/data/mysql.sock password '3307'
[root@elk01 scripts]# mysqladmin -uroot -S /data/3308/data/mysql.sock password '3308'
[root@elk01 scripts]# mysqladmin -uroot -S /data/3309/data/mysql.sock password '3309'
6)查看
[root@elk01 scripts]# mysql -uroot -p3307 -S /data/3307/data/mysql.sock -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 7 |
+---------------+-------+
[root@elk01 scripts]# mysql -uroot -p3308 -S /data/3308/data/mysql.sock -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 8 |
+---------------+-------+
[root@elk01 scripts]# mysql -uroot -p3309 -S /data/3309/data/mysql.sock -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 9 |
+---------------+-------+
[root@elk01 scripts]# vim /usr/local/bin/mysql3307
mysql -uroot -p3307 -S /data/3307/data/mysql.sock
[root@elk01 scripts]# vim /usr/local/bin/mysql3308
mysql -uroot -p3308 -S /data/3308/data/mysql.sock
[root@elk01 scripts]# vim /usr/local/bin/mysql3309
mysql -uroot -p3309 -S /data/3309/data/mysql.sock
[root@elk01 scripts]# chmod +x /usr/local/bin/mysql330*