一、编译安装mysql数据库
yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
useradd -s /sbin/nologin mysql
tar xf mysql-boost-5.7.20.tar.gz
cd mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
make && make install
chown -R mysql.mysql /usr/local/mysql/
vi /etc/my.cnf ===将模板原内容删除,粘贴进去===
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
chown mysql:mysql /etc/my.cnf
echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile (可以识别mysql中的命令)
echo 'export PATH' >> /etc/profile
source /etc/profile
初始化数据库:
cd /usr/local/mysql/
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ 复制系统启动脚本模板
systemctl daemon-reload 刷新识别启动脚本
systemctl start mysqld
netstat -anpt | grep 3306
systemctl enable mysqld
mysqladmin -u root -p password "abc123" //给root账号设置密码
登陆:
mysql -u root -p
授权远程登录:
grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
flush privileges;
二、在编译安装的基础上部署mysql多实例,如果一台服务器上只需要一个 mysql 则不需要部署多实例。多实例是为了节省服务器资源。
1、创建目录
mkdir -p /app/mysql3306/data
mkdir -p /app/mysql3307/data
chown mysql:mysql /app/mysql
2、修改配置文件
mv /etc/my.cnf /etc/my.cnf_bak
cd /etc
cp my.cnf_bak ./my3306.cnf
vi my3306.cnf
[client]
port = 3306
default-character-set=utf8
socket = /app/mysql3306/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /app/mysql3306/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /app/mysql3306/data
port = 3306
character_set_server=utf8
pid-file = /app/mysql3306/mysqld.pid
socket = /app/mysql3306/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
cp my3306.cnf ./my3307.cnf
vi my3307.cnf
[client]
port = 3307
default-character-set=utf8
socket = /app/mysql3307/mysql.sock
[mysql]
port = 3307
default-character-set=utf8
socket = /app/mysql3307/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /app/mysql3307/data
port = 3307
character_set_server=utf8
pid-file = /app/mysql3307/mysqld.pid
socket = /app/mysql3307/mysql.sock
server-id = 2
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
3、增加启动文件
mv /usr/lib/systemd/system/mysqld.service mysqld.service_bak
cd /usr/lib/systemd/system/
cp mysql.service_bak mysqld3306.service
cp mysql.service_bak mysqld3307.service
vi mysqld3306.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
Type=forking
PIDFile=/app/mysql3306/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
#ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd_3306 3306
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my3306.cnf --daemonize --pid-file=/app/mysql3306/mysqld3306.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
vi mysqld3307.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
Type=forking
PIDFile=/app/mysql3307/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
#ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd_3307 3307
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my3307.cnf --daemonize --pid-file=/app/mysql3307/mysqld3307.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
4、初始化
mysqld --defaults-file=/etc/my3306.cnf --initialize --user=mysql --datadir=/app/mysql3306/data
mysqld --defaults-file=/etc/my3307.cnf --initialize --user=mysql --datadir=/app/mysql3307/data
要记住两个临时密码
systemctl start mysqld3306
systemctl start mysqld3307
会卡在,ctrl+c 退出即可
netstat -anptu | grep "mysqld"
tcp6 0 0 :::3307 :::* LISTEN 23681/mysqld
tcp6 0 0 :::3306 :::* LISTEN 23720/mysqld
5、修改登录密码
mysql3306
mysqladmin -p -S /app/mysql3306.sock password #路径要和 /etc/my3306.cnf 里的 socket路径一样
输入临时密码
输入新密码
mysqladmin -p -S /app/mysql3307.sock password
输入临时密码
输入新密码
6、测试登录
mysql -u root -p -S /app/mysql3306.sock
输入密码
mysql -u root -p -S /app/mysql3307.sock
输入密码