二进制包安装mysql
目录
mysql二进制安装部署
1.去官网下载包后上传到服务器
#选择合适的目录上传
[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# ll
total 644556
-rw-------. 1 root root 1260 Dec 29 06:46 anaconda-ks.cfg
-rw-r--r--. 1 root root 660017902 Jan 25 21:52 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
2.创建mysql使用的用户
[root@localhost ~]# groupadd -r -g 306 mysql
[root@localhost ~]# useradd -r -M -s /sbin/nologin -u 306 -g 306 mysql
[root@localhost ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
3.解压到指定安装目录,重命名
#解压到/usr/local
[root@localhost ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.30-linux-glibc2.12-x86_64 sbin share src
重命名,或者为了保留版本号也可以创建软连接,两种都可以
#重命名的方式
[root@localhost ~]# mv /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64/ /usr/local/mysql
#创建软连接的方式
[root@localhost ~]# cd /usr/local
[root@localhost ~]# ln -s mysql-5.7.29-linux-glibc2.12-x86_64 mysql
4.创建数据存放目录
这里选择/data/mysqldata
作为数据目录
[root@localhost ~]# mkdir -p /data/mysqldata
#顺带创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/mysql
5.对相关目录更改属主和属组
[root@localhost ~]# chown -R mysql.mysql /data/mysqldata/
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql/
[root@localhost ~]# chown -R mysql.mysql /var/log/mysql/
6.添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
7.初始化数据库
[root@localhost ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysqldata/
2021-01-26T03:15:29.840234Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-26T03:15:31.180734Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-26T03:15:31.340483Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-26T03:15:31.418839Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: bf4473b5-5f84-11eb-9f2b-000c290c7222.
2021-01-26T03:15:31.419954Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-26T03:15:32.091055Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-26T03:15:32.277964Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
#--initialize-insecure因为这里初始化的是空密码,将后面-insecure去掉应该会生产初始密码的随机字符串(没有尝试过)
#初始化之后mysql的数据目录会 生成一些文件
[root@localhost ~]# ll /data/mysqldata/
total 110660
-rw-r-----. 1 mysql mysql 56 Jan 25 22:15 auto.cnf
-rw-------. 1 mysql mysql 1680 Jan 25 22:15 ca-key.pem
-rw-r--r--. 1 mysql mysql 1112 Jan 25 22:15 ca.pem
-rw-r--r--. 1 mysql mysql 1112 Jan 25 22:15 client-cert.pem
-rw-------. 1 mysql mysql 1680 Jan 25 22:15 client-key.pem
-rw-r-----. 1 mysql mysql 425 Jan 25 22:15 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jan 25 22:15 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jan 25 22:15 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jan 25 22:15 ib_logfile1
drwxr-x---. 2 mysql mysql 4096 Jan 25 22:15 mysql
drwxr-x---. 2 mysql mysql 8192 Jan 25 22:15 performance_schema
-rw-------. 1 mysql mysql 1676 Jan 25 22:15 private_key.pem
-rw-r--r--. 1 mysql mysql 452 Jan 25 22:15 public_key.pem
-rw-r--r--. 1 mysql mysql 1112 Jan 25 22:15 server-cert.pem
-rw-------. 1 mysql mysql 1680 Jan 25 22:15 server-key.pem
drwxr-x---. 2 mysql mysql 8192 Jan 25 22:15 sys
8.编写配置文件my.cnf
[mysqld] #服务端
basedir = /usr/local/mysql #软件安装目录
datadir = /data/mysqldata #数据存放目录
socket = /tmp/mysql.sock #socket文件存放位置
port = 3306 #默认端口
user = mysql #运行用户
skip-name-resolve #跳过名称解析
max_connections=200 # 允许最大连接数
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
skip-character-set-client-handshake
default-storage-engine=INNODB # 创建新表时将使用的默认存储引擎
[mysqld_safe] #这行可以不要,可以全部放在mysqld下
log-error=/var/log/mysql/mysql.log #错误日志
pid-file = /data/mysqldata/mysql.pid #pid文件位置
[client]
default-character-set=utf8mb4
[mysql] #客户端
socket=/tmp/mysql.sock #客户端socket文件存放位置
default-character-set=utf8mb4 # 设置mysql客户端默认字符集
9.配置启动方式
centos6启动脚本
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@localhost mysql]# cd support-files/
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
#复制到/etc/init.d/下
[root@localhost support-files]# cp -a mysql.server /etc/init.d/mysqld
#编辑该启动文件,在46行左右,写上安装目录和数据目录的路径
[root@localhost support-files]# vim /etc/init.d/mysqld
······
basedir=/usr/local/mysql
datadir=/data/mysqldata
······
测试
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/var/log/mysql/mysql.log'.
. SUCCESS!
[root@localhost ~]# service mysqld status
SUCCESS! MySQL running (22192)
[root@localhost ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \q
Bye
#关闭查看
[root@localhost ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@localhost ~]# service mysqld status
ERROR! MySQL is not running
[root@localhost ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::]:22 [::]:*
centos7配置启动脚本
[root@localhost ~]# vim /usr/lib/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
PIDFile=/data/mysqldata/mysql.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/bin/mysqld_pre_systemd
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/data/mysqldata/mysql.pid
# Use this to switch malloc implementation
#EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false
[root@localhost ~]# systemctl daemon-reload
[root@db01 /etc/systemd/system]# systemctl start mysqld.service
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]#
[root@localhost ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.