mysql安装部署
一:部署单实例
1.创建数据目录
mkdir -p /data/mysql_3306/
mkdir -p /data/soft
2.解压安装
cd /data/soft
tar zxf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz -C /opt/
mv /opt/mysql-5.7.28-linux-glibc2.12-x86_64 /opt/mysql-5.7.28
ln -s /opt/mysql-5.7.28 /opt/mysql
3.设置环境变量
echo 'export PATH=$PATH:/opt/mysql/bin' >>/etc/profile
source /etc/profile
mysql -V
4.清除遗留环境
rpm -qa|grep mariadb
yum remove mariadb-libs -y
rm -rf /etc/my.cnf
5.安装mysql依赖包
yum install -y libaio-devel
6.创建mysql用户
useradd -s /sbin/nologin -M mysql
chown -R mysql.mysql /data/
chown -R mysql.mysql /opt/mysql*
7.初始化数据库
mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/data/mysql_3306/
8.创建配置文件
cat > /etc/my.cnf <<EOF
[mysqld]
port=3306
user=mysql
basedir=/opt/mysql
datadir=/data/mysql_3306
socket=/tmp/mysql_3306.sock
[mysql]
socket=/tmp/mysql_3306.sock
EOF
9.启动mysql
cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
systemctl start mysqld
netstat -lntup|grep 3306
10.配置root密码
mysqladmin password -S /tmp/mysql_3306.sock
11.登陆mysql
mysql -uroot -p
二:部署多实例
1.创建数据目录并更改授权
mkdir -p /data/mysql_3307/
mkdir -p /data/mysql_3308/
chown -R mysql.mysql /data/
2.初始化多实例
mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/data/mysql_3307/
mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/data/mysql_3308/
3.创建配置文件
cat > /data/mysql_3307/my.cnf <<EOF
[mysqld]
basedir=/opt/mysql/
datadir=/data/mysql_3307/
socket=/data/mysql_3307/mysql.sock
log_error=/data/mysql_3307/mysql.log
port=3307
EOF
cat > /data/mysql_3308/my.cnf <<EOF
[mysqld]
basedir=/opt/mysql/
datadir=/data/mysql_3308/
socket=/data/mysql_3308/mysql.sock
log_error=/data/mysql_3308/mysql.log
port=3308
EOF
4.创建多实例启动脚本
cat >/etc/systemd/system/mysqld_3307.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/bin/mysqld --defaults-file=/data/mysql_3307/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld_3308.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/bin/mysqld --defaults-file=/data/mysql_3308/my.cnf
LimitNOFILE = 5000
EOF
5.启动多实例
systemctl daemon-reload
systemctl start mysqld_3307.service
systemctl start mysqld_3308.service
6.检查
netstat -lntup|grep mysqld
7.创建密码
mysqladmin -S /data/mysql_3307/mysql.sock password '123456'
mysqladmin -S /data/mysql_3308/mysql.sock password '123456'
8.登陆测试
mysql -S /data/mysql_3307/mysql.sock -uroot -p123456
mysql -S /data/mysql_3308/mysql.sock -uroot -p123456