数据库二进制安装

数据库的下载

# 方法一:yum下载
[root@db01 ~]# cat mysql_install.sh #下载脚本
#!/usr/bin/env bash
cd /opt

wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server

[root@db01 ~]# cat /var/log/mysqld.log | grep password	#查看密码
2021-06-09T06:53:14.191215Z 1 [Note] A temporary password is generated for root@localhost: 
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;

# 方法二:官网二进制安装
[root@db01 app]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

# 卸载掉mariadb
[root@db01 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@db01 ~]# yum remove mariadb-libs-5.5.68-1.el7.x86_64 -y

基础优化

# 创建用户
[root@db01 ~]# useradd -s /sbin/nologin mysql
[root@db01 ~]# id mysql
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql)

# 添加环境变量
[root@db01 ~]# echo export PATH=/app/mysql/bin:$PATH >> /etc/profile.d/hjbl.sh
[root@db01 ~]# bash

# 检测
[root@db01 ~]# mysql -V
mysql  Ver 14.14 Distrib 5.6.51, for linux-glibc2.12 (x86_64) using  EditLine wrapper

理论上讲,数据库的数据应该单独放在一块硬盘上

[root@db01 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part 
  └─centos-root 253:0    0   99G  0 lvm  /
sdb               8:16   0   20G  0 disk 
sdc               8:32   0   20G  0 disk

# 格式化
[root@db01 ~]# mkdir /mysql_data
[root@db01 ~]# mkfs.xfs /dev/sdb 
meta-data=/dev/sdb               isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

# 开机自己挂载
[root@db01 ~]# blkid
/dev/sda1: UUID="52ca67a0-abcb-4505-bdce-1991438e5326" TYPE="xfs" 
/dev/sda2: UUID="hmrPpt-A9i3-uCSF-Xdzc-KfpT-nGba-R1OGW5" TYPE="LVM2_member" 
/dev/sdb: UUID="101fc746-b0a5-45d0-9b29-d0d2a37c5223" TYPE="xfs" 
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos-root: UUID="6af63308-9d8e-460b-9aec-6ae004e40785" TYPE="xfs"

# 在/etc/fatab写入
UUID="101fc746-b0a5-45d0-9b29-d0d2a37c5223" /mysql_data xfs defaults 0 0

# 挂载
[root@db01 ~]# mount -a
[root@db01 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.6M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root   99G  4.0G   95G   4% /
/dev/sda1               1014M  190M  825M  19% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/sdb                  20G   33M   20G   1% /mysql_data

# 授权
[root@db01 /]# chown -R mysql.mysql /usr/local/mysql

# 创建系统数据
[root@db01 scripts]# yum install -y libaio-devel
[root@db01 scripts]# mkdir /usr/local/mysql/data -p
[root@db01 scripts]# chown -R mysql.mysql /usr/local/mysql

# 初始化
# 5.6以上,初始化方式是initialize,5.5以下,参考编译安装的博客
[root@db01 data]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# 指定MySQL,指定工作目录,指定数据存储目录
2021-06-10T10:03:02.648998Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-10T10:03:02.795594Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-06-10T10:03:02.834187Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-10T10:03:02.896964Z 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: 0b402beb-c9d3-11eb-b6ce-000c29db598e.
2021-06-10T10:03:02.898141Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-06-10T10:03:03.565966Z 0 [Warning] CA certificate ca.pem is self signed.
2021-06-10T10:03:03.720636Z 1 [Note] A temporary password is generated for root@localhost: BFY2*/MByY)6

# 不安全初始化,没有初始密码
[root@db01 data]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

[root@db01 ~]# cat > /etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

[mysql]
user="root"
password=""
socket=/tmp/mysql.sock
default-character-set=utf8mb4

[client]
user="root"
password="zkh0928!"
socket=/tmp/mysql.sock
default-character-set=utf8mb4
EOF

# 启动mysqld客户端
[root@db01 bin]# ./mysqld --defaults-file=/etc/my.cnf --user=mysql

# 修改数据库密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;

# 添加环境变量
[root@db01 ~]# vim /etc/profile.d/hjbl.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@db01 ~]# bash
[root@db01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

systemctl管理

[Unit]
Description="这是一个牛逼的数据库"
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=simple
KillMode=control-group
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql
LimitNOFILE = 5000

[Install]
WantedBy=multi-user.target
posted @ 2021-06-29 20:56  BaiM0  阅读(155)  评论(0编辑  收藏  举报