mysql 二进制安装
创建软件目录
# mkdir -p /application
# mv mysql-5.7.20-linux-glibc2.12-x86_64 /application/mysql
修改环境变量
# vim /etc/profile
export PATH=/application/mysql/bin:$PATH
# source /etc/profile
建立MySQL用户组
# useradd mysql
创建相关目录并修改权限
# mkdir /data/mysql -p
# chown -R mysql.mysql /application/*
# chown -R mysql.mysql /data/*
初始化数据(建库)
初始化数据,初始化管理员的临时密码
mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql
默认配置文件
vim /etc/my.cnf
[mysqld]
user=mysql
basedir=/app/mysql
datadir=/data/mysql
server_id=6
port=3306
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
prompt=3306
配置启动脚本
# cd /application/mysql/support-files
# ./mysql.server start
Starting MySQL.Logging to '/data/mysql/db01.err'.
SUCCESS!
cp mysql.server /etc/init.d/mysqld
使用systemd管理mysql
vim /etc/systemd/system/mysqld.service
[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=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
安装方法:
二进制 源码 yum
初始化数据库
mysqld --intialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql
4.简易的配制文件/etc/my.cnf
user basedir datadir server_id port socket
管理员密码
mysqladmin -uroot -p password **
本地管理员忘记密码
# mysqld_safe --skip-grant-tables --skip-networking & //跳过授权
mysql> flush privileges;
mysql> alter user root@'localhost' identified by '123456';
# pkill mysqld
# systemctl start mysqld
多实例
准备多个目录
# mkdir -p /data/330{7,8,9}/data
配置文件
cat > /data/3307/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
log_error=/data/3307/mysql.log
port=3307
server_id=7
log_bin=/data/3307/mysql-bin
EOF
cat > /data/3308/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
socket=/data/3308/mysql.sock
log_error=/data/3308/mysql.log
port=3308
server_id=8
log_bin=/data/3308/mysql-bin
EOF
cat > /data/3309/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3309/data
socket=/data/3309/mysql.sock
log_error=/data/3309/mysql.log
port=3309
server_id=9
log_bin=/data/3309/mysql-bin
EOF
初始化三套数据
mv /etc/my.cnf /etc/my.cnf.bak
mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/application/mysql
systemd管理实例
cd /etc/systemd/system
cp mysqld.service mysqld3307.service
cp mysqld.service mysqld3308.service
cp mysqld.service mysqld3309.service
vi mysqld.3307.service
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
授权
chown -R mysql.mysql /data/*
启动
systemctl start mysqld3307.service
systemctl start mysqld3308.service
systemctl start mysqld3309.service
验证多实例
netstat -lnmp | grep 330
mysql -S /data/3307/mysql.sock -e "select @@server_id";
mysql -S /data/3308/mysql.sock -e "select @@server_id";
mysql -S /data/3309/mysql.sock -e "select @@server_id";
[root@mysql ~]# mysql -S /data/3307/mysql.sock -e "select @@server_id";
+-------------+
| @@server_id |
+-------------+
| 7 |
+-------------+
[root@mysql ~]# mysql -S /data/3308/mysql.sock -e "select @@server_id";
+-------------+
| @@server_id |
+-------------+
| 8 |
+-------------+
[root@mysql ~]# mysql -S /data/3309/mysql.sock -e "select @@server_id";
+-------------+
| @@server_id |
+-------------+
| 9 |
+-------------+