专职DBA-MySQL简介及安装

专职DBA-MySQL简介及安装.docx


课程介绍
MySQL专职DBA课程更新中..........................
要求掌握80%以上相当于DBA中级以上,两年工作经验。
课程大纲:
(1).MySQL5.7二进制安装部署,编译安装自己扩展
(2).MySQL升级步骤扩展
(3).MySQL5.7体系结构原理
(4).MySQL基础管理
(5).基础SQL语句使用
(6).SQL高级应用
(7).Information_schema获取元数据
(8).索引、执行计划管理(基础优化)
(9).存储引擎
(10).日志管理
(11).备份与恢复
(12).主从复制及架构演变
(13).传统的高可用及读写分离(MHA+Atlas)
(14).传统分布式架构设计与实现-扩展(Mycat--->DBLE、DRDS)
(15).MySQL5.7高可用及分布式架构-扩展(MGR、InnoDB Cluster)
(16).MySQL优化(安全、性能)
(17).MySQL 监控(zabbix、Open-falcon)
(18).RDS、DRDS(阿里云课程)
(19).NoSQL-Redis缓存技术
(20).NoSQL-MongoDB分布式数据库技术
(21).PG(了解)
(22).Oracle(了解)


官方定义的MySQL DBA工作职责
(1)开发DBA
懂至少一门开发语言:Java、Python、Golang
基本SQL语句深入学习(增删改查)、数据库结构设计(建模)
高级SQL:存储过程、函数、触发器、视图、事件

(2)运维DBA
初级运维DBA:各版本、各平台安装搭建、升级
中级运维DBA:基础管理(启动关闭、初始化配置文件管理、多实例管理)、基本SQL增删改查、体系结构原理、用户权限管理、索引管理、存储引擎、日志管理、备份恢复、主从复制(构建、状态监控)、故障处理。
高级运维DBA:高性能优化、高可用集群。

以上两点是专职DBA必备技能!!!


DBA的职业素养(更新中。。。。。。)
(1).人品:贩卖数据,泄露数据,严禁恶意删库删表,收起你的好奇心,保护好自己。
(2).态度:天外有天,人外有人,把最简单的事情做得最漂亮。
(3).严谨:做最规范事,理论实践结合,杜绝最佳实践,要有理论支撑或者实践检验(面试问你上一家DBA规范,说几条??)
(4).细心:变更类的操作,危险性操作,多思考。
(5).心态:能顶住压力,别惹事,出事别怕事。
(6).熟悉操作系统:精通各类Linux操作系统。
(7).熟悉公司业务(开发):业务是什么?产品的功能、用户的行为(热功能、热数据),熟悉至少一种开发语言:Python/Golang/Java。
(8).熟悉行业:了解发展趋势,数据库种类、数据库版本、数据库产品类型、什么企业用什么数据库。https://db-engines.com/en/ranking
(9).喜欢数据库:体验很重要,必须真正喜欢数据库。
(10).保持学习:保持每天额外2小时以上学习,熟悉数据库周边技术产品。


DBA精神
责任心、服务心、沟通心、学习心,进取心和分享心,这六大体现正是DBA精神的基石所在。


什么是数据?
数据:文字、图片、视频。。。。。。人类认知的数据表现方式。
计算机:二进制、16进制的机器语言
基于数据的重要性和复杂性的不同,我们可能有不同的管理方式。


哪些数据是适合存储到数据库的呢?
1.重要性比较高的
2.关系较复杂的数据


什么是数据库管理系统(DBMS)?
RDBMS:关系型数据库管理系统
比较适合于,安全级别要求高的数据以及关系较复杂的数据

NoSQL:非关系型数据库管理系统
适合于高性能存取数据,一般是配合RDBMS进行使用的。
针对大数据处理分析,分布式架构更加擅长。


数据库管理系统种类
RDBMS:MySQL 、Oracle、SQL Server、PostgreSQL
NoSQL:Not Only SQL
       键值对(key-value):Redis、Memcached
       文档型(document):MongoDB


MySQL版本企业选择
规范:
GA版本:稳定的发布版本。
版本类型:
    E:企业版,收费版本,SR,偏向传统行业。
    C:社区版,开源版本,互联网企业的选择。
企业版本选择:一般新上的环境,会选择GA版6-12月的产品版本。
企业主流版本:
    5.6版本:5.6.20  5.6.34  5.6.36  5.6.38  5.6.40
    5.7版本:5.7.18  5.7.20  5.7.22  5.7.24
面试问:你们公司用什么版本的数据库呀?
        5.6.38  5.7.20
面试问:你们公司怎么还在用这么低的版本?
        我们这个业务已经运行4年了,一直用的5.6版本,非常稳定。
        所以说暂时没有打算升级。(业务4年圆谎:大版本是5.6,中间有运维做过升级,我在公司不久。)
GA时间问题漏洞怎么说?
        刚去上家公司不到两年,上一个DBA交接的时候业务已经运行的两年多了,我接手的时候mysql的版本已经是5.6.38了,应当是做过小版本的升级。
我们公司现在用的是5.7的,你能胜任吗?
        能。虽然我们生产中一直使用的5.6,但是我对5.7的新特性已经足够了解了。所以我完全能够胜任贵公司的工作。


MySQL分支:
    Oracle MySQL
    MariaDB
    PerconaDB
    RDS AliSQL


MySQL软件下载:GA时间记住
https://downloads.mysql.com/archives/community/

输入:www.mysql.com
点击:downloads
点击:Archives
点击:MySQL Community Server

选择:Product Version:MySQL软件版本5.7.20
选择:Operating System:二进制包、源码包
选择:OS Version:系统版本
------------------------------------------------------------
RPM包:Red Hat Enterprise Linux / Oracle Linux
二进制包:Linux - Generic(不需要二次开发和不需要添加功能)
源码包:Source Code(需要二次开发的时候,需要做统一配置管理的时候)

MySQL是C语言开发的
mysql-5.7.20 GA时间:2017-09-13


硬件服务器选型:
主机:Dell PowerEdge R940
CPU:2路
内存:64G(单根8G,8根)
硬盘:6块500G
      2块做RAID 1安装CentOS 7.4 x86_64和MySQL数据库/app/mysql
      4块做RAID 10存放MySQL数据库数据/data/mysql
网卡:4块网卡做bonding技术


虚拟机模拟做RAID
mdadm命令
参数     作用
-a     检测设备名称
-n     指定设备数量
-l     指定RAID级别
-C     创建
-v     显示过程
-f     模拟设备损坏
-r     移除设备
-Q     查看摘要信息
-D     查看详细信息
-S     停止RAID磁盘阵列
-x     指定备份盘

# mdadm -Cv /dev/md0 -a yes -n 4 -l 10 /dev/sd{c,d,e,f}
# mkfs.xfs /dev/md0
# mkdir /data
# mount /dev/md0 /data
# df -Th
# mdadm -D /dev/md0


CentOS系统安装参考脚本:install_linux_os.sh


企业级网卡绑定技术
[root@db01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
DEVICE=eth0
MASTER=bond0
SLAVE=yes

[root@db01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
DEVICE=eth1
MASTER=bond0
SLAVE=yes

[root@db01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth2
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
DEVICE=eth2
MASTER=bond1
SLAVE=yes

[root@db01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth3
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
DEVICE=eth3
MASTER=bond1
SLAVE=yes

[root@db01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
DEVICE=bond0
IPADDR=10.0.0.51
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
NM_CONTROLLED=no

[root@db01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond1
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
USERCTL=no
DEVICE=bond1
IPADDR=192.168.10.51
NETMASK=255.255.255.0
NM_CONTROLLED=no

[root@db01 ~]# cat /etc/resolv.conf
search db01
nameserver 10.0.0.2
nameserver 223.5.5.5
nameserver 223.6.6.6

[root@db01 ~]# cat /etc/modprobe.d/bond.conf
alias bond0 bonding
options bond0 miimon=100 mode=6
alias bond1 bonding
options bond1 miimon=100 mode=6

[root@db01 ~]# //etc/init.d/network restart
Shutting down interface bond0:                             [  OK  ]
Shutting down interface bond1:                             [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface bond0:                               [  OK  ]
Bringing up interface bond1:                               [  OK  ]

[root@db01 ~]# ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 10.0.0.51  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:fea8:6f2f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a8:6f:2f  txqueuelen 1000  (Ethernet)
        RX packets 3496  bytes 424705 (414.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16536  bytes 1382324 (1.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

bond1: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 192.168.10.51  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fea8:6f43  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a8:6f:43  txqueuelen 1000  (Ethernet)
        RX packets 88  bytes 12470 (12.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12799  bytes 768996 (750.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0c:29:a8:6f:2f  txqueuelen 1000  (Ethernet)
        RX packets 670  bytes 82430 (80.4 KiB)
        RX errors 0  dropped 7  overruns 0  frame 0
        TX packets 9808  bytes 1011200 (987.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0c:29:a8:6f:39  txqueuelen 1000  (Ethernet)
        RX packets 3416  bytes 415187 (405.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7194  bytes 472662 (461.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0c:29:a8:6f:43  txqueuelen 1000  (Ethernet)
        RX packets 32  bytes 5515 (5.3 KiB)
        RX errors 0  dropped 4  overruns 0  frame 0
        TX packets 6433  bytes 386496 (377.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:0c:29:a8:6f:4d  txqueuelen 1000  (Ethernet)
        RX packets 62  bytes 7315 (7.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6366  bytes 382500 (373.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 8  bytes 672 (672.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 672 (672.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@db01 ~]# ping www.baidu.com -c 4
PING www.a.shifen.com (182.61.200.6) 56(84) bytes of data.
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=1 ttl=128 time=13.6 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=2 ttl=128 time=5.17 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=3 ttl=128 time=16.5 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=4 ttl=128 time=5.54 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 5.179/10.232/16.540/4.980 ms

备份网卡文件
[root@db01 ~]# ls -l /etc/sysconfig/network-scripts/ifcfg-*
-rw-r--r--  1 root root 139 Jun 28 23:40 /etc/sysconfig/network-scripts/ifcfg-bond0
-rw-r--r--  1 root root 126 Jun 28 23:32 /etc/sysconfig/network-scripts/ifcfg-bond1
-rw-r--r--  1 root root  88 Jun 28 23:45 /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r--  1 root root  88 Jun 28 23:45 /etc/sysconfig/network-scripts/ifcfg-eth1
-rw-r--r--  1 root root  88 Jun 28 23:35 /etc/sysconfig/network-scripts/ifcfg-eth2
-rw-r--r--  1 root root  88 Jun 28 23:35 /etc/sysconfig/network-scripts/ifcfg-eth3
-rw-r--r--. 1 root root 254 Aug 24  2018 /etc/sysconfig/network-scripts/ifcfg-lo

[root@db01 ~]# zip -r /backup/eth0123.zip /etc/sysconfig/network-scripts/ifcfg-*
  adding: etc/sysconfig/network-scripts/ifcfg-bond0 (deflated 14%)
  adding: etc/sysconfig/network-scripts/ifcfg-bond1 (deflated 7%)
  adding: etc/sysconfig/network-scripts/ifcfg-eth0 (deflated 3%)
  adding: etc/sysconfig/network-scripts/ifcfg-eth1 (deflated 3%)
  adding: etc/sysconfig/network-scripts/ifcfg-eth2 (deflated 5%)
  adding: etc/sysconfig/network-scripts/ifcfg-eth3 (deflated 3%)
  adding: etc/sysconfig/network-scripts/ifcfg-lo (deflated 25%)

[root@db01 ~]# ls -l /backup/
total 4
-rw-r--r-- 1 root root 2243 Jun 29 01:14 eth0123.zip


MySQL数据库二进制安装
[root@db01 ~]# mkdir -p /app

打开CRT或者Xftp上传mysql二进制安装包(建议使用Xftp上传)
[root@db01 ~]# cd /app/
[root@db01 /app]# rz -y
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz...
  100%  626100 KB    13043 KB/sec    00:00:48       0 Errors

[root@db01 /app]# ls -lh
total 612M
-rw-r--r-- 1 root root 612M Jan  1 00:00 mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz


解压mysql二进制包
[root@db01 /app]# tar -xf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz 

[root@db01 /app]# ls -l
total 626104
drwxr-xr-x 9 root root       129 Jun 29 01:27 mysql-5.7.20-linux-glibc2.12-x86_64
-rw-r--r-- 1 root root 641127384 Jan  1 00:00 mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

[root@db01 /app]# du -sh *
2.5G    mysql-5.7.20-linux-glibc2.12-x86_64
612M    mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

[root@db01 /app]# mv mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz /disk/soft/

[root@db01 /app]# mv mysql-5.7.20-linux-glibc2.12-x86_64/ mysql

[root@db01 /app]# ls -l
total 0
drwxr-xr-x 9 root root 129 Jun 29 01:27 mysql

[root@db01 /app]# cd mysql/
[root@db01 /app/mysql]# ls -l
total 36
drwxr-xr-x  2 root root   4096 Jun 29 01:27 bin
-rw-r--r--  1 7161 31415 17987 Sep 13  2017 COPYING
drwxr-xr-x  2 root root     55 Jun 29 01:27 docs
drwxr-xr-x  3 root root   4096 Jun 29 01:27 include
drwxr-xr-x  5 root root    229 Jun 29 01:27 lib
drwxr-xr-x  4 root root     30 Jun 29 01:27 man
-rw-r--r--  1 7161 31415  2478 Sep 13  2017 README
drwxr-xr-x 28 root root   4096 Jun 29 01:27 share
drwxr-xr-x  2 root root     90 Jun 29 01:27 support-files


修改环境变量
[root@db01 ~]# vim /etc/profile
export PATH=/app/mysql/bin:$PATH
:wq
[root@db01 ~]# source /etc/profile


查看MySQL版本
[root@db01 ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.20, for linux-glibc2.12 (x86_64) using  EditLine wrapper


卸载mariadb数据库的依赖包
[root@db01 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@db01 ~]# yum remove mariadb-libs -y


创建mysql用户和组
[root@db01 ~]# id mysql
id: mysql: no such user
[root@db01 ~]# useradd -s /sbin/nologin -M mysql
[root@db01 ~]# id mysql
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql)


[root@db01 ~]# mkdir -p /data
[root@db01 ~]# chown -R mysql:mysql /data
[root@db01 ~]# chown -R mysql:mysql /app


检查mysql依赖包是否安装
[root@db01 ~]# rpm -qa | grep ncurses-devel
ncurses-devel-5.9-14.20130511.el7_4.x86_64

[root@db01 ~]# rpm -qa | grep libaio
libaio-devel-0.3.109-13.el7.x86_64
libaio-0.3.109-13.el7.x86_64

[root@db01 ~]# rpm -qa | grep gcc
gcc-c++-4.8.5-36.el7.x86_64
libgcc-4.8.5-36.el7.x86_64
gcc-4.8.5-36.el7.x86_64
gcc-gfortran-4.8.5-36.el7.x86_64

如果你没有安装依赖包就用yum装
[root@db01 ~]# yum install ncurses-devel libaio libaio-devel gcc gcc-c++ -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest version
Package libaio-0.3.109-13.el7.x86_64 already installed and latest version
Package libaio-devel-0.3.109-13.el7.x86_64 already installed and latest version
Package gcc-4.8.5-36.el7_6.2.x86_64 already installed and latest version
Package gcc-c++-4.8.5-36.el7_6.2.x86_64 already installed and latest version
Nothing to do


初始化数据库
5.6版本的初始化数据库,初始化完成后:无密码和无安全策略的)
/app/mysql/scripts/mysql_install_db \
--user=mysql \
--basedir=/app/mysql \
--datadir=/data/mysql \


5.7版本的初始化数据库,有两种初始化方式:
(1).第一种初始化方式:开启临时密码和安全策略。
初始化数据,初始化管理员的临时密码。
/app/mysql/bin/mysqld --initialize \
--user=mysql \
--basedir=/app/mysql \
--datadir=/data/mysql \

(2).第二种初始化方式和之前一样,是无密码无安全策略的。
初始化数据,初始化管理员的密码为空。
/app/mysql/bin/mysqld --initialize-insecure \
--user=mysql \
--basedir=/app/mysql \
--datadir=/data/mysql \


安全策略???
密码的复杂度进行了要求,对于密码的过期时间设置了限制。

新特性重要说明:
5.7开始MySQL加入了全新的密码安全机制:
1.初始化完成后会生成临时密码(显示到屏幕上,并且会往日志中记一份)
2.密码复杂度:长度:超过12位?复杂度:字符混乱组合。
3.密码过期时间180天。


开始初始化数据库
[root@db01 ~]# /app/mysql/bin/mysqld --initialize-insecure \
> --user=mysql \
> --basedir=/app/mysql \
> --datadir=/data/mysql \
2019-06-28T17:51:12.363922Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-28T17:51:12.780289Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-28T17:51:12.864221Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-28T17:51:12.924660Z 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: 51ae7e15-99cd-11e9-a44b-000c29a86f2f.
2019-06-28T17:51:12.926919Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-28T17:51:12.929988Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.


初始化参数介绍:
--initialize-insecure \ 是用无安全限制的方式进行数据初始化
--user=mysql \ 设置数据的管理用户
--basedir=/app/mysql \ 软件的安装路径
--datadir=/data/mysql \ 数据的存储位置


[root@db01 ~]# cd /data/mysql/
[root@db01 /data/mysql]# ls -l
total 110628
-rw-r----- 1 mysql mysql       56 Jun 29 01:51 auto.cnf
-rw-r----- 1 mysql mysql      419 Jun 29 01:51 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Jun 29 01:51 ibdata1
-rw-r----- 1 mysql mysql 50331648 Jun 29 01:51 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Jun 29 01:51 ib_logfile1
drwxr-x--- 2 mysql mysql     4096 Jun 29 01:51 mysql
drwxr-x--- 2 mysql mysql     8192 Jun 29 01:51 performance_schema
drwxr-x--- 2 mysql mysql     8192 Jun 29 01:51 sys


书写默认配置文件
[root@db01 ~]# 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=mysql [\\d]> \


配置启动脚本
[root@db01 ~]# cd /app/mysql/support-files/
[root@db01 /app/mysql/support-files]# ls -l
total 24
-rw-r--r-- 1 mysql mysql   773 Sep 13  2017 magic
-rwxr-xr-x 1 mysql mysql  1061 Sep 14  2017 mysqld_multi.server
-rwxr-xr-x 1 mysql mysql   894 Sep 14  2017 mysql-log-rotate
-rwxr-xr-x 1 mysql mysql 10576 Sep 14  2017 mysql.server

[root@db01 /app/mysql/support-files]# cp mysql.server /etc/init.d/mysqld
[root@db01 /app/mysql/support-files]# cd
[root@db01 ~]# ls -l /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10576 Jun 29 01:57 /etc/init.d/mysqld
[root@db01 ~]# chmod 700 /etc/init.d/mysqld
[root@db01 ~]# ls -l /etc/init.d/mysqld
-rwx------ 1 root root 10576 Jun 29 01:57 /etc/init.d/mysqld


启动数据库
[root@db01 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/db01.err'.
 SUCCESS!


检查进程端口信息
[root@db01 ~]# netstat -tunlp | grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      33742/mysqld        

[root@db01 ~]# ss -tunlp | grep mysql
tcp    LISTEN     0      80       :::3306                 :::*                   users:(("mysqld",pid=33742,fd=20))

[root@db01 ~]# ps -elf | grep mysql
4 S root     33576     1  0  80   0 - 28328 do_wai 01:59 pts/0    00:00:00 /bin/sh /app/mysql/bin/mysqld_safe --datadir=/data/mysql/ --pid-file=/data/mysql//db01.pid
4 S mysql    33742 33576  0  80   0 - 281344 poll_s 01:59 pts/0   00:00:00 /app/mysql/bin/mysqld --basedir=/app/mysql --datadir=/data/mysql --plugin-dir=/app/mysql/lib/plugin --user=mysql --log-error=db01.err --pid-file=/data/mysql//db01.pid --socket=/tmp/mysql.sock --port=3306
0 R root     33800  6386  0  80   0 - 28177 -      02:00 pts/0    00:00:00 grep --color=auto mysql

[root@db01 ~]# lsof -i :3306
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  33742 mysql   20u  IPv6  87103      0t0  TCP *:mysql (LISTEN)

[root@db01 ~]# lsof -i :mysql
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  33742 mysql   20u  IPv6  87103      0t0  TCP *:mysql (LISTEN)


使用systemd管理mysql启动
[root@db01 ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Service]
User=mysql
Group=mysql
ExecStart=/app/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

[Install]
WantedBy=multi-user.target


使用systemd启动mysql之前,先将原来模式启动mysqld先关闭
[root@db01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@db01 ~]# ss -tunlp | grep mysql
[root@db01 ~]#


[root@db01 ~]# systemctl status mysqld.service 
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           https://dev.mysql.com/doc/refman/en/using-systemd.html


[root@db01 ~]# systemctl enable mysqld.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@db01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/mysqld.service 
lrwxrwxrwx 1 root root 38 Jun 29 02:22 /etc/systemd/system/multi-user.target.wants/mysqld.service -> /usr/lib/systemd/system/mysqld.service


[root@db01 ~]# systemctl start mysqld.service 
[root@db01 ~]# systemctl status mysqld.service 
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-06-29 02:23:40 CST; 5s ago
     Docs: man:mysqld(8)
           https://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 34169 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─34169 /app/mysql/bin/mysqld --defaults-file=/etc/my.cnf

Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.615863Z 0 [Note]   - '::' resolves to '::';
Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.615875Z 0 [Note] Server socket created on IP: '::'.
Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.618347Z 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql/ib_buffer_pool
Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.620480Z 0 [Note] InnoDB: Buffer pool(s) load completed at 190629  2:23:40
Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.629209Z 0 [Note] Event Scheduler: Loaded 0 events
Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.630331Z 0 [Note] /app/mysql/bin/mysqld: ready for connections.
Jun 29 02:23:40 db01 mysqld[34169]: Version: '5.7.20'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)
Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.630343Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get...s check.
Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.630346Z 0 [Note] Beginning of list of non-natively partitioned tables
Jun 29 02:23:40 db01 mysqld[34169]: 2019-06-28T18:23:40.643196Z 0 [Note] End of list of non-natively partitioned tables
Hint: Some lines were ellipsized, use -l to show in full.


[root@db01 ~]# ss -tunlp | grep mysql
tcp    LISTEN     0      80       :::3306                 :::*                   users:(("mysqld",pid=34169,fd=16))


[root@db01 ~]# ps -elf | grep mysql
4 S mysql    34169     1  0  80   0 - 281410 poll_s 02:23 ?       00:00:00 /app/mysql/bin/mysqld --defaults-file=/etc/my.cnf
0 R root     34242  6386  0  80   0 - 28177 -      02:28 pts/0    00:00:00 grep --color=auto mysql


socket文件
[root@db01 ~]# ls -l /tmp/
total 4
srwxrwxrwx 1 mysql mysql 0 Jun 29 02:23 mysql.sock
-rw------- 1 mysql mysql 6 Jun 29 02:23 mysql.sock.lock


登录数据库
[root@db01 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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 [(none)]> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql [(none)]> exit;
Bye


设置数据库root密码为123
[root@db01 ~]# mysqladmin -uroot -p password 123
Enter password: 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.


[root@db01 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@db01 ~]# mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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 [(none)]> exit;
Bye


[root@db01 ~]# mysql -uroot -p -S /tmp/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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 [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

密码字段authentication_string
mysql [(none)]> select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

mysql [(none)]> exit;
Bye
[root@db01 ~]#

猜你喜欢

转载自www.cnblogs.com/zhouwanchun/p/11104449.html