一、添加一块硬盘用来存放MySQL的数据
二、对硬盘进行分区格式化挂载
1.分区 格式化
[root@localhost ~]# fdisk /dev/nvme0n3
[root@localhost ~]# mkfs.xfs /dev/nvme0n3p1
2创建data挂载目录
[root@localhost ~]# mkdir /data
3.编辑etc/fastab实现自动挂载
[root@localhost ~]# blkid /dev/nvme0n3p1 //获取UUID的值
/dev/nvme0n3p1: UUID="e7e0d760-1ca1-47b7-a9a0-0a4abe6c6184" TYPE="xfs" PARTUUID="ab065a8d-01"
[root@localhost ~]# vim /etc/fstab
UUID=e7e0d760-1ca1-47b7-a9a0-0a4abe6c6184 /data xfs defaults 0 0
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.7M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 37G 7.5G 30G 21% /
/dev/nvme0n1p1 1014M 180M 835M 18% /boot
/dev/sr0 7.4G 7.4G 0 100% /mnt/cdrom
/dev/nvme0n2p1 100G 1.4G 99G 2% /share
tmpfs 376M 1.2M 375M 1% /run/user/42
tmpfs 376M 4.0K 376M 1% /run/user/0
/dev/nvme0n3p1 20G 176M 20G 1% /data
三、准备安装包并上传至linux(各种方法都可行,VMware文件共享也可以,直接在linux中下载也可以,这里我搭建好了samba 就直接用samba将安装包传到Linux中)(直接看第四步)
放一个samba配置。samba配置
[root@localhost ~]# cat /etc/samba/smb.conf
See smb.conf.example for a more detailed config file or # read the smb.conf manpage. # Run 'testparm' to verify the config is correct after # you modified it.
[global]
workgroup = WORKGROUP
security = user
map to guest = Bad user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775i
[myshare]
comment = myshare
path = /share
writable=yes
guest ok =yes
browseable = yes
[root@localhost ~]#
四、创建一个目录来解压MySQL安装包。
[root@localhost ~]# mkdir /app
[root@localhost ~]# tar -xzvf /share/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C /app/ //-C指定解压位置
[root@localhost ~]# mv /app/mysql-5.7.26-linux-glibc2.12-x86_64/ /app/mysql //改个目录名字
五、创建MySQL用户
[root@localhost app]# useradd -s /sbin/nologin mysql
六、设置环境变量并测试
[root@localhost ~]# vim /etc/profile
export PATH=/app/mysql/bin:$PATH //最后一行加
[root@localhost ~]# source /etc/profile
[root@localhost ~]# mysql -V
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
可以看到这报错了 少一个组件,这里使用yum 安装一下即可。
[root@localhost ~]# yum install libnc*
[root@localhost ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
七、给文件夹授予MySQL用户权限
[root@localhost ~]# chown -R mysql:mysql /app/mysql/*
[root@localhost ~]# chown -R mysql:mysql /data
八、 初始化数据库
[root@localhost ~]# mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/
[root@localhost ~]# ls /data/
auto.cnf ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema sys
九、创建编辑my. cnf文件
[root@localhost ~]# vim /etc/my.cnf
#服务器端配置
[mysqld]
#用户
user=mysql
#软件安装目录
basedir=/app/mysql
#数据路径
datadir=/data
#socket文件位置
socket=/tmp/mysql.sock
#服务器id号
server_id=6
#端口号
port=3306
#客户端配置
[mysql]
#socket文件位置
socket=/tmp/mysql.sock
十、使用systemd管理数据库
[root@localhost ~]# 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=/app/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
十一、使用systemd启动MySQL服务
[root@localhost ~]# systemctl start mysqld.service //启动服务
[root@localhost ~]# systemctl status mysqld.service //查看状态
[root@localhost ~]# systemctl enable mysqld.service //加入启动项
十二、连接数据库
1.本地连接
[root@localhost ~]# mysqladmin -uroot -p password //设置密码
[root@localhost ~]# mysql -uroot -p //使用密码连接
2.使用客户端连接
出现这个问题的原因是因为没有给root开放远程登录的权限。这里我设置任何主机可登录。
先使用本地登录进去
[root@localhost ~]# mysql -uroot -p123
mysql> use mysql;
mysql> update user set host='%' where user ='root';
mysql> flush privileges;
连接成功!