MySQL 数据库基础实践
使用MySQL是不二的选择,接着就跟着超哥学起来。。。
版本选择
企业版
MySQL 企业版由MySQL AB公司内部专门的人员负责开发及维护,但同时也会吸纳社区人员编写的优秀代码及算法,并且由他们严格按照软件测试流程对这些采纳的代码进行测试。确定没有问题之后才会进行发布。
简单地说,MySQL企业版是由MySQL公司内部发布的,它参考了社区版的先进代码功能和算法,是MySQL公司的盈利产品,需要付费才能使用及提供服务支持,稳定性和可靠性无疑都是最好的,当然了,企业腰包得够鼓买得起。某知名分类门户网站2008年 就购买过MySQL企业版,价格不比那些闭源的商业数据库便宜,也是大几十万。
社区版
MySQL 社区版则是由分散在世界各地的MySQL开发者,爱好者以及用户参与开发与测试的,包括软件代码的管理,测试工作,也是他们在负责。社区也会设立BUG汇报机制。收集用户在使用过程中遇到的BUG情况,相比于企业版,社区版的开发及测试环境没有那么严格。
选哪个
mysql是成熟产品,企业版和社区版在性能方面区别不大,对于我们学习而言,社区版即可。它们的区别可以如下了解:
-
企业版对代码的管理,测试更严格,稳定性更好
-
企业版不遵循DPL开源协议,而社区版遵循,可以免费用
-
企业版可以购买额外的收费服务,如7*24的技术支持,有钱任性。
-
社区版的安全性,稳定性,无法像企业版有及时的维护,技术支持。
MySQL特点(C/S架构即client-server)
支持多种操作系统,Windows,MacOS,Linux等支持多种语言API,如C,C++,Python,PHP,Java等
API: 提供给开发者的一个接口,它的作用就是能够让开发非常方便地通过代码去管理,去获取我们数据库的信息
支持多线程,充分利用硬件资源 支持多种存储引擎
mysql 就是一个基于 socket 编写的 C/S 架构的软件
socket指的就是linux本地的一个套接字文件,说白了就是在内存中进行数据共享的
客户端软件 mysql 自带:如mysql 命令,mysqldump命令等 python模块:如 pymysql(通过第三方框架去连接mysql数据库)
-
MySQL服务端-客户端
先看下什么是B/S 和 C/S 架构
B/S是Browser/Server 指浏览器和服务器端,在客户机不需要安装软件,只需要装一个浏览器。就可以访问服务器 比如百度,搜狐,新浪等等都是B/S架构 只要本地有浏览器,能上网,输入你要访问服务器的网址就可以访问了。
C/S是Client/Server指客户端和服务器,在客户端必须装客户端软件及相应的环境后,才能访问服务器 比如QQ,微信等等都是C/S架构 你要使用QQ,必须在你的机器上安装客户端软件及相应的环境,你才可以使用QQ聊天。
MySQL是基于客户端-服务端的运行模式数据库,服务端 负载数据处理,运行在数据库服务器上。
用户通过发送增删改查等请求,发送给 客户端软件,然后通过网络提交请求给 服务端,服务端接收到请求,再进行处理,然后返回。
服务端,客户端可以在不同的机器上,也可以在一台机器上。
这种服务端,客户端,就在生活里很常见,如打游戏时的登录,QQ,微信的登录,MySQL 也是一个登录的过程
mysql下载选择
http://mirrors.sohu.com/mysql/
准备环境 机器环境
192.168.51.247 tech_master01 root 123123 centos7
192.168.51.69 tech_slave01 root 123123 centos7
192.168.51.185 tech_slave02 root 123123 centos7
修改主机名
# 查看主机名
[root@MiWiFi-RA80-srv ~]# hostname
MiWiFi-RA80-srv
# 修改主机名
[root@MiWiFi-RA80-srv ~]# hostnamectl set-hostname tech_master01
# 重启再次查看主机名
[root@MiWiFi-RA80-srv ~]# reboot
[root@tech_master01 ~]# hostname
tech_master01
我们看主机名已经成功修改了
安装MySQL 5.6 的方式
安装方式有哪些?
1. rpm包安装
2,yum方式安装
3.源码编译安装
rpm方式
http://mirrors.sohu.com/mysql/MySQL-5.6/
http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.49.tar.gz
https://dev.mysql.com/downloads/repo/yum/
https://repo.mysql.com//mysql80-community-release-el7-9.noarch.rpm
https://repo.mysql.com/
https://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/ #mysql官网仓库
注意,进入第三方搜狐源码仓库,为了快速定位到需要的版本,可以按 快捷键 ctrl + F 可以看到 页面搜索框 把自己需要的参数输入框中,回车就可以定位到自己需要的 然后右键 复制链接就可以下载源码包了。如下图所示:
下载好如下 rpm 包
# 分别下载 mysql-client mysql-devel mysql-server mysql-shared 4个 rpm 包
wget http://mirrors.sohu.com/mysql/MySQL-5.6/MySQL-shared-5.6.49-1.el7.x86_64.rpm
wget http://mirrors.sohu.com/mysql/MySQL-5.6/MySQL-devel-5.6.49-1.el7.x86_64.rpm
# 安装 rpm 包
rpm -ivh xxxx.rpm
rpm安装必须要提前准备好官网,第三方提供好的 rpm 软件包,且无法满足定制化需求,编译参数,修改路径,依赖冲突问题,所以不建议使用。
-
这是最方便的,yum 自动去下载相关 rpm 包 centos7 中已经不存在mysql 已经全部换成mariadb
-
因此要安装数据库,用 yum 命令直接输入 yum install mariadb-server mariadb -y 就可以了 ,一个是客户端 mariadb 一个是服务端 mariadb-server 就安装他们,就可以快速使用 mysql 数据库了
-
yum安装指定的版本
还得去配置比如 mysql5.6 的 yum 下载源
# centos7 里面的mysql 下载yum 源就是如下版本,指定版本还得另想办法 如上 去配置 yum 下载源
[root@lcds ~]# yum install mariadb-server mariadb -y
正在安装:
mariadb x86_64 1:5.5.68-1.el7 base 8.8 M
mariadb-server x86_64 1:5.5.68-1.el7 base 11 M
# 还回去安装你的电脑缺少的依赖 这些信息是 yum 根据你的电脑自动寻找的,不同的电脑依赖有可能不一样
# 安装结束以后,可以用 systemctl 去管理 mariadb 数据库 跟 mysql 用 yum 安装一样
systemctl start mariadb
systemctl status mariadb
systemctl stop mariadb
# 查看防火墙
[root@lcds ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
INPUT_direct all -- anywhere anywhere
# 执行功能
[root@lcds ~]# getenforce
Enforcing
Disabled(老师的)
-
编译安装 mysql 5.6 重点
源代码编译安装三步曲
1.获取 mysql 代码源码包, 解压缩,源码目录下有一个 makefile 文件
2.进入 mysql 的源码目录下,执行 make 指令,自动读取 makefile 文件
## 对 makefile 解释:
程序员写好的 makefile ,编译安装 mysql,它需要依赖操作系统哪些软件包,才能正确去运行
3. 在make 编译完成之后,执行 make install,安装 mysql 到指定位置
编译步骤
编译安装需要下载源码包,可以定制化编译参数,路径等信息
http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.40.tar.gz
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.49.tar.gz
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.50.tar.gz
# 编译参数如下
tar zxf mysql-5.6.40.tar.gz
cd mysql-5.6.40
# 提示,编译时可配置的选项很多,具体可参考本章最后一部分的内容或官方文档。
make
make install
ln -s /application/mysql-5.6.50/ /application/mysql
参数详解|命令详解
cmake.-DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.60 \ # 老师的
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql # 别人的
# 指定 MYSQL 安装路径 程序存放位置
-DMYSQL_DATADIR=/application/mysql-5.6.50/data \ # 老师的
-DMYSQL_DATADIR=/data/mysql_db #别人的
# 指定 MYSQL 数据存储路径 数据存放位置
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.50/tmp/mysql.sock \
# 指定 MYSQL.SOCK 文件路径 socket文件存放位置,需要提前创建好 tmp 文件
-DDEFAULT_CHARSET=utf8 \ # 默认字符为UTF-8
-DDEFAULT_COLLATION=utf8_general_ci \ # 校验字符
###-DWITH_EXTRA_CHARSETS=all \ ####
-DEXTRA_CHARSETS=all \ # 安装所有扩展字符集
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ # 安装 Innodb 存储引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1 \ # 开启 Federated 引擎
-DWITH_BLACKHOLE_STORAGE_ENGINF=1 \ # 安装 blackhole 存储引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ # 不启用EXAMPLE存储引擎
-DWITH_ZLIB=bundled \ # 启用libz库支持
### -DWITH_SSL=bundled \ ###
-DENABLED_LOCAL_INFILE=1 \ #启用本地数据导入支持
-DWITH_EMBEDDED_SERVER=1 \ # 编译嵌入式服务器支持
-DENABLE_DOWNLOADS=1 \ # mysql5.6支持google的c++mock框架了,允许下载,否则安装会报错
-DWITH_SSL=system \ #使用用系统的SSL库
-DWITH_READLINE=1 \ # 启用 readline 库支持
-DINSTALL_MANDIR=share/man # 手册目录
-DWITH_DEBUG=0 #禁用debug
开始安装且配置mysql-5.6
环境准备
1.vmware虚拟机内存建议4G => virtualbox 虚拟机内存 4096MB linux的配置,CPU至少2颗 这里是4颗
2.编译安装过程中,注意看是否有 error 等信息即不得出现 error 关键字
3.需要安装好一些编译所需的基础环境
4.超哥用centos7给大家讲课
# 查询 操作系统版本
[root@tech_master01 ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
# 查询 操作系统内核版本
[root@tech_master01 ~]# uname -r
3.10.0-1160.el7.x86_64
# 查询 操作系统架构
[root@tech_master01 ~]# uname -m
x86_64
# 查询 linux 的具体情况
[root@tech_master01 ~]# uname -a
Linux tech_master01 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
# 查看 tech_master01这台机器的内存大小
[root@tech_master01 ~]# free -m
total used free shared buff/cache available
Mem: 3773 内存 258 3376 8 137 3326
Swap: 2047 0 2047
-
基础依赖
-
基础依赖如下: yum -y install gcc gcc-c++ autoconf make cmake automake openssl openssl-devel pcre pcre-devel zlib zlib-devel ncurses-devel libaio* libmecab* libnumal libssl-dev perl perl-* wget vim curl lezsz net-tools GCC:用于编译和构建MySQL的源代码。 yum install gcc openssl-devel:提供用于SSL支持的OpenSSL库。 yum install openssl-devel zlib-devel:提供用于压缩、解压缩和处理数据的zlib库。 yum install zlib-devel bison:是一种语法分析器生成器,用于生成MySQL的解析器。yum install bison ncurses-devel:提供用于构建MySQL客户端的ncurses库。 编译前的依赖安装 mysql依赖这个两个工具 `ncurses-devel libaio-devl`,是mysql编译安装正确运行必须的依赖 yum install ncurses-devel libaio-devel gcc gcc+* make cmake wget curl bison zlib-devel openssl-devel perl perl-* # 安装依赖 命令如下: [root@tech_master01 ~]# yum install ncurses-devel libaio-devel gcc gcc-c++ make cmake wget curl perl perl-* zlib-devel openssl-devel bison # 查看是否正确安装了 rpm -qa ncurses-devel libaio-devel 这两个工具包 [root@tech_master01 ~]# rpm -qa ncurses-devel libaio-devel ncurses-devel-5.9-14.20130511.el7_4.x86_64 libaio-devel-0.3.109-13.el7.x86_64 表示这两个工具包已经正确安装 # 这里是采用 cmake 编译,安装,所以再执行 yum install cmake [root@tech_master01 ~]# yum install cmake # 查询是否正确安装 cmake [root@tech_master01 ~]# rpm -qa cmake cmake-2.8.12.2-2.el7.x86_64 表示已经正确安装了 cmake ---------------依赖已经安装结束-------------------------- # 创建mysql用户,不让登录bash # 创建mysql用户,用于授权目录 useradd -s /sbin/nologin -M mysql [root@tech_master01 ~]# useradd -s /sbin/nologin -M mysql [root@tech_master01 ~]# id mysql uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)
下载源码包
获取 mysql 的源代码 # 创建 目录 mkdir -p/home/mary/tools/ # 创建一个文件夹,统一管理我们下载的第三方 代码包 [root@tech_master01 ~]# mkdir -p /home/mary/tools/ [root@tech_master01 ~]# cd /home/mary/tools/ [root@tech_master01 tools]# # 创建一个 [root@tech_master01 ~]# mkdir -p /application/mysql-5.6.49/tmp/ # 安装wget [root@tech_master01 tools]# yum install wget -y # 查看wget是否正确安装 [root@tech_master01 tools]# rpm -qa wget wget-1.14-18.el7_6.1.x86_64 表示已经成功安装了wget [root@tech_master01 tools]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.49.tar.gz --2023-08-13 12:57:14-- http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.49.tar.gz Resolving mirrors.sohu.com (mirrors.sohu.com)... 123.125.123.141 Connecting to mirrors.sohu.com (mirrors.sohu.com)|123.125.123.141|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 32402099 (31M) [application/octet-stream] Saving to: ‘mysql-5.6.49.tar.gz’ 100%[==============================================================================>] 32,402,099 125KB/s in 4m 52s 2023-08-13 13:02:07 (108 KB/s) - ‘mysql-5.6.49.tar.gz’ saved [32402099/32402099] [root@tech_master01 tools]# ll total 31644 -rw-r--r--. 1 root root 32402099 Jun 2 2020 mysql-5.6.49.tar.gz
执行cmake,生成makefile,用于编译安装
-
编译参数
[root@tech_master01 mysql-5.6.50]# cmake.-DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.50 \ -DMYSQL_DATADIR=/application/mysql-5.6.50/data \ -DMYSQL_UNIX_ADDR=/application/mysql-5.6.50/tmp/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_EXTRA_CHARSETS=all \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ -DWITH_ZLIB=bundled \ -DWITH_SSL=bundled \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_EMBEDDED_SERVER=1 \ -DENABLE_DOWNLOADS=1 \ -DWITH_DEBUG=0
验证mysql安装 如下所示
[root@tech_master01 mysql-5.6.49]#ls /application/
mysql-5.6.49
[root@tech_master01 mysql-5.6.49]# cd /application/
[root@tech_master01 application]# ls
mysql-5.6.49
# 创建软件链接
[root@tech_master01 application]# ln -s /application/mysql-5.6.49/ /application/mysql
# 查软链接是否成功
[root@tech_master01 ~]# ls /application/mysql-5.6.50/
bin data docs include lib LICENSE man mysql-test README scripts share sql-bench support-files
[root@tech_master01 ~]# ls /application/mysql
bin data docs include lib LICENSE man mysql-test README scripts share sql-bench support-files
[root@tech_master01 ~]# ls /application/
mysql mysql-5.6.50
[root@tech_master01 ~]# ll /application/
总用量 0
lrwxrwxrwx. 1 root root 26 8月 14 13:15 mysql -> /application/mysql-5.6.50/
drwxr-xr-x. 13 root root 191 8月 14 13:14 mysql-5.6.50
#查看mysql的bin目录里面的二进制文件有哪些?
[root@tech_master01 ~]# ls /application/mysql/bin
innochecksum mysql mysql_client_test mysqld_safe mysqlimport mysqltest_embedded resolveip
msql2mysql mysqlaccess mysql_client_test_embedded mysqldump mysql_plugin mysql_tzinfo_to_sql resolve_stack_dump
myisamchk mysqlaccess.conf mysql_config mysqldumpslow mysql_secure_installation mysql_upgrade
myisam_ftdump mysqladmin mysql_config_editor mysql_embedded mysql_setpermission mysql_waitpid
myisamlog mysqlbinlog mysql_convert_table_format mysql_find_rows mysqlshow mysql_zap
myisampack mysqlbug mysqld mysql_fix_extensions mysqlslap perror
my_print_defaults mysqlcheck mysqld_multi mysqlhotcopy mysqltest replace
[root@tech_master01 ~]#
# 查看软连接
[root@tech_master01 ~]# ls /application//mysql #查看软连接
bin data docs include lib LICENSE man mysql-test README scripts share sql-bench support-files
[root@tech_master01 ~]# ll /application/ # 软连接指向的目录
总用量 0
lrwxrwxrwx. 1 root root 26 8月 14 13:15 mysql -> /application/mysql-5.6.50/
drwxr-xr-x. 13 root root 191 8月 14 13:14 mysql-5.6.50
[root@tech_master01 ~]#
初始化数据库
1.进入到mysql目录 查看目录中的内容 找到 scripts 目录 并进入该目录 找到 mysql_install_db这个二进制文件 这个文件就是用于数据库初始化操作文件
# 进入到mysql目录 查看目录中的内容 找到 scripts 目录
[root@tech_master01 ~]# cd /application/mysql
[root@tech_master01 mysql]# ls
bin data docs include lib LICENSE man mysql-test README scripts share sql-bench support-files
[root@tech_master01 mysql]# cd scripts
# 查看mysql_install_db这个二进制文件 这个文件就是用于数据库初始化操作文件
[root@tech_master01 scripts]# ll
总用量 36
-rwxr-xr-x. 1 root root 34977 8月 14 12:02 mysql_install_db
[root@tech_master01 scripts]#
# 查看当前的路径
[root@tech_master01 scripts]# pwd
/application/mysql/scripts
[root@tech_master01 scripts]# ls
mysql_install_db
# 查看mysql数据文件目录 里面只有一个test目录,里面没有数据,这表示当前 mysql 它是一个空的 我们得对他进行初始化操作,让它生成相应的数据,我们才可以去使用这个 mysql ,具体的初始化命令如下:
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/
--datadir=/application/mysql/data --user=mysql
#初始化命令解释:初始化绝对路径的初始化命令/application/mysql/scripts/mysql_install_db 设定mysql的软连接目录 =>/application/mysql/,指定mysql初始化数据往哪里写=>/application/mysql/data --user 指定初始化 mysql数据库的用户是 mysql这个用户 去降低他的权限。
# 执行如下初始化命令,保证参数和老师一致
[root@tech_master01 scripts]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
出现两个 ok 表示初始化正常
# 再次查看 /application/mysql/data/
# 以及看到如下数据目录,表示 mysql 初始化正确
[root@tech_master01 scripts]# ls /application/mysql/data/
ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
###对上解释 :
生成了一个存储用户账号和密码的 mysql 库
还生成了一个用于性能测试的 performance_schema 库
还有一个用于测试的 test 库
[root@tech_master01 ~]# ls /application/mysql/data/
test
[root@tech_master01 ~]# ls /application/mysql/data/test/
db.opt
[root@tech_master01 ~]#
-
mysql数据初始化操作如下命令:
[root@tech_master01 scripts]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
Installing MySQL system tables...2023-08-14 14:51:56 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-08-14 14:51:56 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2023-08-14 14:51:56 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.50) starting as process 20950 ...
2023-08-14 14:51:56 20950 [Note] InnoDB: Using atomics to ref count buffer pool pages
2023-08-14 14:51:56 20950 [Note] InnoDB: The InnoDB memory heap is disabled
2023-08-14 14:51:56 20950 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2023-08-14 14:51:56 20950 [Note] InnoDB: Memory barrier is not used
2023-08-14 14:51:56 20950 [Note] InnoDB: Compressed tables use zlib 1.2.11
2023-08-14 14:51:56 20950 [Note] InnoDB: Using Linux native AIO
2023-08-14 14:51:56 20950 [Note] InnoDB: Using CPU crc32 instructions
2023-08-14 14:51:56 20950 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2023-08-14 14:51:56 20950 [Note] InnoDB: Completed initialization of buffer pool
2023-08-14 14:51:56 20950 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2023-08-14 14:51:56 20950 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2023-08-14 14:51:56 20950 [Note] InnoDB: Database physically writes the file full: wait...
2023-08-14 14:51:56 20950 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2023-08-14 14:51:56 20950 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2023-08-14 14:51:57 20950 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2023-08-14 14:51:57 20950 [Warning] InnoDB: New log files created, LSN=45781
2023-08-14 14:51:57 20950 [Note] InnoDB: Doublewrite buffer not found: creating new
2023-08-14 14:51:57 20950 [Note] InnoDB: Doublewrite buffer created
2023-08-14 14:51:57 20950 [Note] InnoDB: 128 rollback segment(s) are active.
2023-08-14 14:51:57 20950 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-08-14 14:51:57 20950 [Note] InnoDB: Foreign key constraint system tables created
2023-08-14 14:51:57 20950 [Note] InnoDB: Creating tablespace and datafile system tables.
2023-08-14 14:51:57 20950 [Note] InnoDB: Tablespace and datafile system tables created.
2023-08-14 14:51:57 20950 [Note] InnoDB: Waiting for purge to start
2023-08-14 14:51:57 20950 [Note] InnoDB: 5.6.50 started; log sequence number 0
2023-08-14 14:51:57 20950 [Note] RSA private key file not found: /application/mysql/data//private_key.pem. Some authentication plugins will not work.
2023-08-14 14:51:57 20950 [Note] RSA public key file not found: /application/mysql/data//public_key.pem. Some authentication plugins will not work.
2023-08-14 14:51:57 20950 [Note] Binlog end
2023-08-14 14:51:57 20950 [Note] InnoDB: FTS optimize thread exiting.
2023-08-14 14:51:57 20950 [Note] InnoDB: Starting shutdown...
2023-08-14 14:51:59 20950 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
Filling help tables...2023-08-14 14:51:59 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-08-14 14:51:59 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2023-08-14 14:51:59 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.50) starting as process 21066 ...
2023-08-14 14:51:59 21066 [Note] InnoDB: Using atomics to ref count buffer pool pages
2023-08-14 14:51:59 21066 [Note] InnoDB: The InnoDB memory heap is disabled
2023-08-14 14:51:59 21066 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2023-08-14 14:51:59 21066 [Note] InnoDB: Memory barrier is not used
2023-08-14 14:51:59 21066 [Note] InnoDB: Compressed tables use zlib 1.2.11
2023-08-14 14:51:59 21066 [Note] InnoDB: Using Linux native AIO
2023-08-14 14:51:59 21066 [Note] InnoDB: Using CPU crc32 instructions
2023-08-14 14:51:59 21066 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2023-08-14 14:51:59 21066 [Note] InnoDB: Completed initialization of buffer pool
2023-08-14 14:51:59 21066 [Note] InnoDB: Highest supported file format is Barracuda.
2023-08-14 14:51:59 21066 [Note] InnoDB: 128 rollback segment(s) are active.
2023-08-14 14:51:59 21066 [Note] InnoDB: Waiting for purge to start
2023-08-14 14:51:59 21066 [Note] InnoDB: 5.6.50 started; log sequence number 1625977
2023-08-14 14:51:59 21066 [Note] RSA private key file not found: /application/mysql/data//private_key.pem. Some authentication plugins will not work.
2023-08-14 14:51:59 21066 [Note] RSA public key file not found: /application/mysql/data//public_key.pem. Some authentication plugins will not work.
2023-08-14 14:51:59 21066 [Note] Binlog end
2023-08-14 14:51:59 21066 [Note] InnoDB: FTS optimize thread exiting.
2023-08-14 14:51:59 21066 [Note] InnoDB: Starting shutdown...
2023-08-14 14:52:01 21066 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/application/mysql//bin/mysqladmin -u root password 'new-password'
/application/mysql//bin/mysqladmin -u root -h tech_master01 password 'new-password'
Alternatively you can run:
/application/mysql//bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd . ; /application/mysql//bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /application/mysql//my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
-
准备启动 mysql 管理脚本
之前自己写的启停管理脚本
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
bindir="/application/mysql/bin"
datadir="/application/mysql/data"
mysqld_pid_file_path="/application/mysql/`hostname` .pid"
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin" # 此步对开机启动及定时启动及其关键。
export PATH
return_value=0
# lock directory. 对结果的美化
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
log_success_msg() {
echo "SUCCESS! $@" #注意函数的缩进,下同,也是专业的表现,可放到 functions 里。
}
log_failure_msg() {
echo "ERROR! $@"
}
# Start Func
start() {
# Start daemon
each "Starting MySQL"
if test -X $bindir/mysqld_safe # 启动文件是否可执行。
then
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_path" >/dev/null &
return_value=$? # 是否处理好返回值是区别脚本是否专业规范的关键。
sleep 2
# Make lock for CentOS
if test -W "$lockdir" # 锁目录是否可写。
then
touch "$lock_file_path" # 创建锁文件。
fi
exit $return_value
else
log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
fi
}
# Stop Func
stop() {
if test -s "$mysqld_pid_file_path" # 是否PID文件存在并大小大于0.
then
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -O $mysqld_pid 2>/dev/null) # 检查PID对应的进程是否存在
then
echo "Shutting down MySQL"
kill $mysqld_pid # 不能带 -9,否则后果自负。
return_value=$?
sleep 2
else
log_failure_msg "MySQL server process #$mysqld_pid is not running!"
rm -f "$mysqld_pid_file_path"
fi
# Delete lock for oldboy's CentOS
if test -f "$lock_file_path"
then
rm -f "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "MySQL server PID file could not be found!"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
if $0 stop; then
$0 start
else
log_failure_msg "Failed to stop running server,so refusing to try to start."
exit 1
fi
;;
*)
echo "Usage:$0 {start|stop|restart}"
exit 1
esac
exit $return_value # 是否处理好返回值是区别脚本专业规范的关键
使用 mysql 初始化自动生成的管理脚本
# 将系统初始化自动生成的启停脚本目录 拷贝 到系统自带的脚本目录( /etc/init.d/ )来,
# 并取名文mysqld 执行如下命令:
cp support-files/mysql.server /etc/init.d/mysqld
[root@tech_master01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
# 权限 加上一个可执行权限 命令如下:
[root@tech_master01 mysql]# chmod 700 /etc/init.d/mysqld
#
准备启动 mysql
# 提前准备配置文件如果你不做,你下面的路就走不下去了即这个一步你不做,就可能出错了
# 启动需要什么,当然是提前准备好 mysql 的配置文件 my.cnf 我们只需要拷贝 support/my-default.cnf (这是 mysql 初始化时自动生成的默认配置文件,我们只需要拿来用就OK) 到我们的 配置文件 my.cnf 就可以了 即 cp support-files/my-default.cnf /etc/my.cnf 执行如下命令:
[root@tech_master01 mysql]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? yes
# 调用 /etc/init.d/mysqld 脚本去 status 执行如下命令:
[root@tech_master01 mysql]# /etc/init.d/mysqld status
ERROR! MySQL is not running
# 默认情况下,会出现如下错误
[root@tech_master01 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/application/mysql-5.6.50/data/tech_master01.err'.
230814 18:32:11 mysqld_safe Directory '/application/mysql-5.6.50/tmp' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/application/mysql-5.6.50/data/tech_master01.pid).
# 是因为缺少 tmp 目录,创建即可 mkdir -p /application/mysql/tmp 执行这条命令
[root@tech_master01 mysql]# mkdir -p /application/mysql/tmp
# 再次启动数据库
# 发现这里还是没有启动成功,是因为权限问题
[root@tech_master01 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/application/mysql-5.6.50/data/tech_master01.err'.
.. ERROR! The server quit without updating PID file (/application/mysql-5.6.50/data/tech_master01.pid).
# 查看 mysql 里面的内容
[root@tech_master01 mysql]# ls -l /application/mysql
lrwxrwxrwx. 1 root root 26 8月 14 13:15 /application/mysql -> /application/mysql-5.6.50/
# 以下内容显示除了data目录用户,用户组是mysql外,其他读书root,导致权限错误
[root@tech_master01 mysql]# ls -l /application/mysql/
总用量 228
drwxr-xr-x. 2 root root 4096 8月 14 13:13 bin
drwxr-xr-x. 5 mysql mysql 151 8月 14 18:48 data
drwxr-xr-x. 2 root root 55 8月 14 13:13 docs
drwxr-xr-x. 3 root root 4096 8月 14 13:13 include
drwxr-xr-x. 3 root root 4096 8月 14 13:13 lib
-rw-r--r--. 1 root root 198041 9月 23 2020 LICENSE
drwxr-xr-x. 4 root root 30 8月 14 13:13 man
-rw-r--r--. 1 root root 943 8月 14 14:51 my.cnf
drwxr-xr-x. 10 root root 4096 8月 14 13:14 mysql-test
-rw-r--r--. 1 root root 587 9月 23 2020 README
drwxr-xr-x. 2 root root 30 8月 14 13:13 scripts
drwxr-xr-x. 28 root root 4096 8月 14 13:14 share
drwxr-xr-x. 4 root root 4096 8月 14 13:14 sql-bench
drwxr-xr-x. 2 root root 136 8月 14 17:44 support-files
drwxr-xr-x. 2 root root 6 8月 14 18:45 tmp
[root@tech_master01 mysql]#
# 把子目录中的用户 用户组全部换成 mysql mysql 即执行 chown -R mysql,mysql /application/mysql/ 这条命令
[root@tech_master01 mysql]# chown -R mysql.mysql /application/mysql/
[root@tech_master01 mysql]#
# 再次查看 ls -l /application/mysql/
# 发现 /application/mysql/ 目录下的所有目录和文件的用户和用户组都是mysql 即属主,属组都更换成功
[root@tech_master01 mysql]# ls -l /application/mysql/
总用量 228
drwxr-xr-x. 2 mysql mysql 4096 8月 14 13:13 bin
drwxr-xr-x. 5 mysql mysql 151 8月 14 18:48 data
drwxr-xr-x. 2 mysql mysql 55 8月 14 13:13 docs
drwxr-xr-x. 3 mysql mysql 4096 8月 14 13:13 include
drwxr-xr-x. 3 mysql mysql 4096 8月 14 13:13 lib
-rw-r--r--. 1 mysql mysql 198041 9月 23 2020 LICENSE
drwxr-xr-x. 4 mysql mysql 30 8月 14 13:13 man
-rw-r--r--. 1 mysql mysql 943 8月 14 14:51 my.cnf
drwxr-xr-x. 10 mysql mysql 4096 8月 14 13:14 mysql-test
-rw-r--r--. 1 mysql mysql 587 9月 23 2020 README
drwxr-xr-x. 2 mysql mysql 30 8月 14 13:13 scripts
drwxr-xr-x. 28 mysql mysql 4096 8月 14 13:14 share
drwxr-xr-x. 4 mysql mysql 4096 8月 14 13:14 sql-bench
drwxr-xr-x. 2 mysql mysql 136 8月 14 17:44 support-files
drwxr-xr-x. 2 mysql mysql 6 8月 14 18:45 tmp
[root@tech_master01 mysql]#
# 再次尝试启动 mysql 发现终于启动成功了
[root@tech_master01 mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
# 查看启动详细信息
[root@tech_master01 mysql]# netstat -tunlp | grep mysql
tcp6 0 0 :::3306 :::* LISTEN 11070/mysqld
上面属于 mysql 服务器 mysql.server 已经正确启动了
登录 mysql 数据库
去客户端连接 mysql, 进行数据库的进行数据增删改查
# 使用 mysql 命令连接
# 参数后面不用加空格
# -uroot -u是指定用户名,紧挨着用户名
# -p紧挨着密码,一般不会在命令行写入密码。否则容易通过历史记录看到数据库密码,不安全
mysql -uroot -p
[root@tech_master01 ~]# mysql -uroot -p
-bash: mysql: command not found
表示数据库是安装好了,还差一个pqth变量的修改
修改mysql 的 path 变量
# 打印当前变量
[root@tech_master01 ~]# echo $PATH
/root/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@tech_master01 mysql]# echo $PATH
/root/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# 以下两种方式修改mysql 变量
# 1.在 /etc/profile 文件末尾添加如下一句
export PATH=/application/mysql/bin:$PATH
# 2.或者‘export PATH=/application/mysql/bin:$PATH' >> /etc/profile 就可以了
[root@tech_master01 mysql]# vim /etc/profile
# 源码加载配置文件,使修改生效
[root@tech_master01 mysql]# source /etc/profile
[root@tech_master01 mysql]#
# 再次查看系统变量 mysql 变量已经生效了
[root@tech_master01 mysql]# echo $PATH
/application/mysql/bin:/root/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# 查看写入情况
[root@tech_master01 ~]# tail -5 /etc/profile
# 写入PATH,加入 mysql 的路径
export PATH=/application/mysql/bin:$PATH
# 再次使用 mysql -uroot -p 登录 mysql 数据库
[root@tech_master01 mysql]# mysql -uroot -p
Enter password: # 默认是空密码,直接回车即可登录成功 进入的数据库交互界面
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.50 Source distribution
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>
# 查询数据库 发现有四个数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql>
# 为了数据库的安全考虑,一般我们会把测试 test 数据库删除
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
# mysql退出交互式界面命令如下:
mysql> exit;
Bye
[root@tech_master01 mysql]#
mysql安全配置
# mysql 默认没有密码,很不安全,因此要修改密码
# mysql默认提供 mysqladmin命令,用于修改数据库信息
# 修改数据库密码
[root@tech_master01 mysql]# mysqladmin -uroot -p password
Enter password: # 直接回车(mysql默认是没有密码的,所以直接回车)
New password: # 输入你自己设定的新密码
Confirm new password: # 再次输入你自己设定的新密码
# --hlocalhost 表示登录的主机
[root@tech_master01 mysql]# mysql -uroot -p -hlocalhost
Enter password: # 这里报错表示没有输入密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@tech_master01 mysql]# mysql -uroot -p -hlocalhost
Enter password: # 这里报错表示密码错误
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
# 下面表示成功登录mysql 数据库 只有输入正确的密码才能登录 mysql
[root@tech_master01 mysql]# mysql -uroot -p -hlocalhost
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.50 Source distribution
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>
至此,源码安装mysql-5.6数据库就结束了