简介
MySQL是一个关系型数据库管理系统,由瑞典 Mysql AB 公司开发,属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一。
三大分支
- MySQL MySQL
- Mariadb MariaDB Foundation - MariaDB.org
- Percona Server Open Source Database Software Support & Services | Percona
提高安全性
MySQL5.6前版本需要进行安全加固,运行脚本:mysql_secure_installation。
脚本位置:/usr/bin/mysql_secure_installation
脚本内容:设置MySQL管理员root密码、禁止root远程登录、删除anonymous用户账号、删除test数据库
客户端程序
- mysql:mysql: 基于mysql协议交互式或非交互式的CLI工具
- mysqladmin:基于mysql协议管理mysqld
- mysqldump:备份工具,基于mysql协议向mysqld发起查询,并将查询到的数据转换成insert等写语句保存到文本文件中。
- mysqlimport:数据导入工具
MyISAM引擎的数据库(MySQL5.5版本以前)
- myisamchk:检查MyISAM库
- myisampack:打包MyISAM表,只读
服务器端程序
- mysqld:MySQL 的服务器守护程序(daemon
- mysqld_safe:是一个启动脚本,用于启动 mysqld 守护程序
- mysqld_multi:是一个工具,管理多个 MySQL 或 MariaDB 服务器实例
用户账号
mysql的用户账户由两部分组成,'username'@'host',如:wenzi@'192.168.28.10'
支持通配符
- %:匹配任意长度的任意字符。如 192.168.28.% 表示192.168.28网段
- _:匹配任意单个字符
命令格式
mysql [选项] [数据库]
选项:
A, --no-auto-rehash 禁止补全
u, --user= 用户名,默认为root
-h, --host= 服务器主机,默认为localhost
-p, --passowrd= 用户密码,建议使用-p,默认为空密码
-P, --port= 服务器端口
-S, --socket= 指定连接socket文件路径
-D, --database= 指定默认数据库
-C, --compress 启用压缩
-e "SQL" 不登录数据库执行SQL命令
-V, --version 显示版本
-v --verbose 显示详细信息
--print-defaults 获取程序默认使用的配置
切换数据库
use 数据库名;
查看当前数据库
select database();
查看当前用户
select user();
查看数据库所有用户
select user,host from mysql.user;
mysqladmin [选项] [命令]
查看mysql服务是否正常,如果正常提示 mysqld is active
mysqladmin -uroot -pAdmin. ping
创建数据库
mysqladmin -uroot -pAdmin. create cs
修改root密码
mysqladmin -uroot -pAdmin. password 'newpassword'
日志滚动
mysqladmin -uroot -pAdmin. flush-logs
修改mysql提示符
部分版本mysql进入数据库后不显示当前位置,可人为修改
临时修改
mysql -uroot -pcentos --prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
永久修改
vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_" #12小进制
prompt="\\R:\\m:\\s(\\u@\\h) [\\d]>\\_" #24小时制
配置所有mysql客户端自动登录
vim /etc/my.cnf.d/client.cnf
[client]
user=用户名
password=密码
使用模式
交互模式:在mysql命令行执行的语句末尾必须以分号 ; 结尾。如 select * version();
脚本模式:
- mysql -uroot -pAdmin. < sql文件绝对路径.sql
- cat sql文件绝对路径,sql | mysql -uroot -pAdmin.
- 在mysql命令行 mysql>source sql文件绝对路径.sql
服务器端配置文件
命令行
mysql> select @@sql_log_bin;
mysql> set sql_log_bin=0;
文件
- /etc/my.cnf:全局配置
- /etc/mysql/my.cnf:全局配置
- ~/.my.cnf:用户级别的配置文件
格式:
[mysqld]
[mysqld_safe]
[mysqld_multi]
[mysql]
[mysqladmin]
[mysqldump]
[server]
[client]
其中:1、on、true意义相同;0、off、true意义相同;不区分大小写。_和-相同
socket连接
服务器监听的两种 socket 地址:
- ip socket:监听在tcp的3306端口,支持远程通信 ,侦听3306/tcp端口可以在绑定有一个或全部接口IP上
- unix sock: 监听在sock文件上,仅支持本机通信, 如:/var/lib/mysql/mysql.sock)。说明:host为localhost 时自动使用unix sock
关闭mysql网络连接,只侦听本地客户端, 所有客户端和服务器的交互都通过一个socket文件实现。
socket默认位置: /var/lib/mysql/mysql.sock ,可通过/etc/my.cnf修改
vim /etc/my.cnf
[mysqld]
skip-networking=1
bind_address=127.0.0.1
安装部署
yum源安装
mysql | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
[root@wenzi ~]#vim /etc/yum.repos.d/mysql-community.repo
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[root@wenzi ~]# yum -y install mysql-community-server
[root@wenzi ~]# systemctl start mysqld
修改密码
#查看默认密码
[root@wenzi ~]# grep "password" /var/log/mysqld.log
2023-09-04T17:54:42.391661Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: xax<ayT_e9Zs
[root@wenzi ~]# mysql -uroot -p'xax<ayT_e9Zs'
#修改root密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin.123';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
#使用新密码登录
[root@wenzi ~]# mysql -uroot -p'Admin.123'
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 10
Server version: 8.0.34 MySQL Community Server - GPL
...
二进制安装(生产常用)
安装记录一(推荐)
适用于mysql5.7和mysql8.0
安装依赖等相关包
yum -y install libaio numactl-libs ncurses-conmpat-libs
创建mysql专用组和用户
groupadd mysql && useradd -r -g mysql -s /bin/false mysql
准备程序文件
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
tar -zx -f mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local
ln -s mysql-5.7.29-linux-glibc2.12-x86_64/ mysql
chown -R mysql:mysql mysql
配置环境变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
准备配置文件
cp /etc/my.cnf{,.bak}
vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
初始化数据库并提取初始密码
mkdir -p /data/mysql
mysqld --initialize --user=mysql --datadir=/data/mysql
awk '/temporary password/{print $NF}' /data/mysql/mysql.log
准备服务脚本并启动服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
修改mysql登录密码
mysqladmin -uroot -p'初始密码' password 新密码
登录验证
mysql -uroot -p新密码
适用于mysql5.6
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -d /data/mysql mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
tar -x -f mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -s mysql-5.6.50-linux-glibc2.12-x86_64 mysql
chown -R mysql:mysql /usr/local/mysql
cd /usr/local/mysql
cp -a support-files/my-default.cnf /etc/my.cnf
vim /etc/my.cnf
[mysqld]
user=mysql
datadir = /data/mysql
innodb_file_per_table = on #每个InnoDB表使用单独的.idb文件存储索引和数据。在mariadb5.5以上版的是默认值,可不加
skip_name_resolve = on #禁止主机名解析,建议使用
socket=/tmp/mysql.sock
symbolic-links=0
log-bin
pid-file=/data/mysql/mysqld.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
cd /usr/local/mysql
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
ll /data/mysql/
总用量 110600
-rw-rw---- 1 mysql mysql 12582912 2月 14 18:00 ibdata1
-rw-rw---- 1 mysql mysql 50331648 2月 14 18:00 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 2月 14 18:00 ib_logfile1
drwx------ 2 mysql mysql 4096 2月 14 18:00 mysql
drwx------ 2 mysql mysql 4096 2月 14 18:00 performance_schema
drwx------ 2 mysql mysql 6 2月 14 18:00 test
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
执行安全加固脚本,中途回车、y、输入root密码
/usr/local/mysql/bin/mysql_secure_installation
安装记录二
官方文档 MySQL :: MySQL 8.1 Reference Manual :: 2.2 Installing MySQL on Unix/Linux Using Generic Binaries
通用Unix/Linux二进制包MySQL安装布局
目录 | 目录内容 |
---|---|
bin | mysqld server, client 和实用程序 |
docs | MySQL info格式手册 |
man | Unix 手册页 |
include | 包括头文件 |
lib | 库 |
share | 错误消息、字典和用于数据库安装的SQL |
support-files | 杂项支持文件 |
[root@wenzi ~]# ls
mysql-8.0.34-linux-glibc2.28-x86_64.tar.gz
#MySQL依赖libaio库
[root@wenzi ~]# yum search libaio
[root@wenzi ~]# yum -y install libaio
[root@wenzi ~]# tar -zx -f mysql-8.0.34-linux-glibc2.28-x86_64.tar.gz -C /usr/local/
[root@wenzi ~]# ln -s /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/ /usr/local/mysql
[root@wenzi ~]# ls /usr/local/mysql
bin docs include lib LICENSE man README share support-files
#创建mysql专用用户和组
[root@wenzi ~]# groupadd mysql
[root@wenzi ~]# useradd -r -g mysql -s /bin/false mysql
#删除系统自带的mysql或mariadb的配置文件,以免影响后续安装
[root@wenzi mysql]# rm -f /etc/my.cnf
#配置环境变量
[root@wenzi ~]# vim /etc/profile.d/mysql80.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@wenzi ~]# source /etc/profile.d/mysql80.sh
[root@wenzi ~]# cd /usr/local/mysql/
#这个文件必须有,暂时不知作用
[root@wenzi mysql]# mkdir mysql-files
[root@wenzi mysql]# chown mysql:mysql mysql-files
[root@wenzi mysql]# chmod 750 mysql-files
[root@wenzi mysql]# chown -R mysql:mysql /usr/local/mysql/
#初始化
[root@wenzi mysql]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.11' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.22' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by mysqld)
mysqld: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by mysqld)
mysqld: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by mysqld)
mysqld: /lib64/libc.so.6: version `GLIBC_2.25' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libcrypto.so.3)
mysqld: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
出现报错,大致意思说找不到相应版本的glibc,错误信息中glibc版本最高位2.28;查看现在系统glibc版本
#现系统上所有的glibc版本,可见最高为2.17,不满足条件,和报错信息符合
[root@wenzi ~]# strings /lib64/libc.so.6 | grep "GLIBC"
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_2.6
GLIBC_2.7
GLIBC_2.8
GLIBC_2.9
GLIBC_2.10
GLIBC_2.11
GLIBC_2.12
GLIBC_2.13
GLIBC_2.14
GLIBC_2.15
GLIBC_2.16
GLIBC_2.17
GLIBC_PRIVATE
...
[root@wenzi mysql]# yum info glibc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
#已安装的版本
Installed Packages
Name : glibc
Arch : x86_64
Version : 2.17
Release : 317.el7
Size : 13 M
Repo : installed
From repo : anaconda
Summary : The GNU libc libraries
URL : http://www.gnu.org/software/glibc/
License : LGPLv2+ and LGPLv2+ with exceptions and GPLv2+
Description : The glibc package contains standard libraries which are used by
: multiple programs on the system. In order to save disk space and
: memory, as well as to make upgrading easier, common system code is
: kept in one place and shared between programs. This particular package
: contains the most important sets of shared libraries: the standard C
: library and the standard math library. Without these two libraries, a
: Linux system will not function.
去下载glibc_2.28,地址 Index of /gnu/glibc ,编译安装glibc2.28
[root@wenzi ~]# curl -O http://ftp.gnu.org/gnu/glibc/glibc-2.28.tar.xz
[root@wenzi ~]# tar -Jx -f glibc-2.28.tar.xz -C /usr/local/
[root@wenzi ~]# mkdir /usr/local/glibc-2.28/build
[root@wenzi ~]# cd /usr/local/glibc-2.28/build
[root@wenzi build]# ../configure --prefix=/usr/ --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/glibc-2.28/build':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
出现报错,在$PATH中找不到可接受的C编译器,查看日志 cat config.log
应该与gcc有关,查看本机是否安装gcc
安装gcc
[root@wenzi build]# yum -y install gcc
[root@wenzi build]# ../configure --prefix=/usr/ --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for gcc... gcc
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for readelf... readelf
checking for g++... no
checking for c++... no
checking for gpp... no
checking for aCC... no
checking for CC... no
checking for cxx... no
checking for cc++... no
checking for cl.exe... no
checking for FCC... no
checking for KCC... no
checking for RCC... no
checking for xlC_r... no
checking for xlC... no
checking whether we are using the GNU C++ compiler... no
checking whether g++ accepts -g... no
checking whether g++ can link programs... no
checking for sysdeps preconfigure fragments... aarch64 alpha arm hppa i386 m68k microblaze mips nios2 powerpc riscv s390 sh sparc x86_64 checking whether gcc compiles in -mx32 mode by default... no
checking for use of fpu sysdeps directories... yes
checking for -fstack-protector... yes
checking for -fstack-protector-strong... yes
checking for -fstack-protector-all... yes
checking for assembler and linker STT_GNU_IFUNC support... yes
checking for gcc attribute ifunc support... yes
checking if compiler warns about alias for function with incompatible types... no
checking sysdep dirs... sysdeps/unix/sysv/linux/x86_64/64 sysdeps/unix/sysv/linux/x86_64 sysdeps/unix/sysv/linux/x86 sysdeps/x86/nptl sysdeps/unix/sysv/linux/wordsize-64 sysdeps/x86_64/nptl sysdeps/unix/sysv/linux sysdeps/nptl sysdeps/pthread sysdeps/gnu sysdeps/unix/inet sysdeps/unix/sysv sysdeps/unix/x86_64 sysdeps/unix sysdeps/posix sysdeps/x86_64/64 sysdeps/x86_64/fpu/multiarch sysdeps/x86_64/fpu sysdeps/x86/fpu sysdeps/x86_64/multiarch sysdeps/x86_64 sysdeps/x86 sysdeps/ieee754/float128 sysdeps/ieee754/ldbl-96 sysdeps/ieee754/dbl-64/wordsize-64 sysdeps/ieee754/dbl-64 sysdeps/ieee754/flt-32 sysdeps/wordsize-64 sysdeps/ieee754 sysdeps/generic
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln -s works... yes
checking whether /usr/bin/as is GNU as... yes
checking whether /usr/bin/ld is GNU ld... yes
checking for /usr/bin/as... /usr/bin/as
checking version of /usr/bin/as... 2.27, ok
checking for /usr/bin/ld... /usr/bin/ld
checking version of /usr/bin/ld... 2.27, ok
checking for gnumake... no
checking for gmake... gmake
checking version of gmake... 3.82, bad
checking for gnumsgfmt... no
checking for gmsgfmt... no
checking for msgfmt... msgfmt
checking version of msgfmt... 0.19.8.1, ok
checking for makeinfo... no
checking for sed... sed
checking version of sed... 4.2.2, ok
checking for gawk... gawk
checking version of gawk... 4.0.2, ok
checking for bison... no
checking if gcc -B/usr/bin/ is sufficient to build libc... no
checking for nm... nm
checking for python3... no
checking for python... python
configure: error:
*** These critical programs are missing or too old: make bison compiler
*** Check the INSTALL file for required versions.
出现报错,这些关键的程序要么缺少,要么太老了:make bison编译器;检查INSTALL文件以获取所需的版本。
下载make,地址 Index of /gnu/make
[root@wenzi ~]# curl -O https://ftp.gnu.org/gnu/make/make-4.2.tar.gz
[root@wenzi ~]# ls
anaconda-ks.cfg glibc-2.28.tar.xz make-4.2.tar.gz mysql-8.0.34-linux-glibc2.28-x86_64.tar.gz original-ks.cfg
[root@wenzi ~]# tar -xzvf make-4.2.tar.gz
[root@wenzi ~]# cd make-4.2
[root@wenzi make-4.2]# ls
ABOUT-NLS ChangeLog configure.bat function.c hash.c Makefile.ami NMakefile README.OS2 strcache.c vmsify.c
acinclude.m4 commands.c COPYING getloadavg.c hash.h makefile.com os.h README.VMS subproc.bat vmsjobs.c
aclocal.m4 commands.h debug.h getopt1.c implicit.c Makefile.DOS output.c README.W32 tests vms_progname.c
alloca.c config default.c getopt.c INSTALL Makefile.in output.h remake.c variable.c vpath.c
amiga.c config.ami dep.h getopt.h job.c makefile.vms po remote-cstms.c variable.h w32
amiga.h configh.dos dir.c gettext.h job.h makeint.h posixos.c remote-stub.c version.c
ar.c config.h.in doc glob loadapi.c make.lnk read.c rule.c vmsdir.h
arscan.c config.h-vms dosbuild.bat gmk-default.h load.c make_msvc_net2003.sln README rule.h vms_exit.c
AUTHORS config.h.W32 expand.c gmk-default.scm main.c make_msvc_net2003.vcproj README.Amiga SCOPTIONS vms_export_symbol.c
build.sh.in configure file.c gnumake.h make.1 misc.c README.customs signame.c vms_export_symbol_test.com
build_w32.bat configure.ac filedef.h guile.c Makefile.am NEWS README.DOS SMakefile vmsfunctions.c
[root@wenzi make-4.2]# ./configure
[root@wenzi make-4.2]# make
[root@wenzi make-4.2]# make install
[root@wenzi make-4.2]# rm -rf /usr/bin/make
[root@wenzi make-4.2]# cp ./make /usr/bin/
[root@wenzi make-4.2]# make -v
GNU Make 4.2
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[root@wenzi ~]# yum -y install bison
再次编译安装glibc2.28
[root@wenzi ~]# cd /usr/local/glibc-2.28/build/
[root@wenzi build]# ../configure --prefix=/usr/ --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for gcc... gcc
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for readelf... readelf
checking for g++... no
checking for c++... no
checking for gpp... no
checking for aCC... no
checking for CC... no
checking for cxx... no
checking for cc++... no
checking for cl.exe... no
checking for FCC... no
checking for KCC... no
checking for RCC... no
checking for xlC_r... no
checking for xlC... no
checking whether we are using the GNU C++ compiler... no
checking whether g++ accepts -g... no
checking whether g++ can link programs... no
checking for sysdeps preconfigure fragments... aarch64 alpha arm hppa i386 m68k microblaze mips nios2 powerpc riscv s390 sh sparc x86_64 checking whether gcc compiles in -mx32 mode by default... no
checking for use of fpu sysdeps directories... yes
checking for -fstack-protector... yes
checking for -fstack-protector-strong... yes
checking for -fstack-protector-all... yes
checking for assembler and linker STT_GNU_IFUNC support... yes
checking for gcc attribute ifunc support... yes
checking if compiler warns about alias for function with incompatible types... no
checking sysdep dirs... sysdeps/unix/sysv/linux/x86_64/64 sysdeps/unix/sysv/linux/x86_64 sysdeps/unix/sysv/linux/x86 sysdeps/x86/nptl sysdeps/unix/sysv/linux/wordsize-64 sysdeps/x86_64/nptl sysdeps/unix/sysv/linux sysdeps/nptl sysdeps/pthread sysdeps/gnu sysdeps/unix/inet sysdeps/unix/sysv sysdeps/unix/x86_64 sysdeps/unix sysdeps/posix sysdeps/x86_64/64 sysdeps/x86_64/fpu/multiarch sysdeps/x86_64/fpu sysdeps/x86/fpu sysdeps/x86_64/multiarch sysdeps/x86_64 sysdeps/x86 sysdeps/ieee754/float128 sysdeps/ieee754/ldbl-96 sysdeps/ieee754/dbl-64/wordsize-64 sysdeps/ieee754/dbl-64 sysdeps/ieee754/flt-32 sysdeps/wordsize-64 sysdeps/ieee754 sysdeps/generic
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln -s works... yes
checking whether /usr/bin/as is GNU as... yes
checking whether /usr/bin/ld is GNU ld... yes
checking for /usr/bin/as... /usr/bin/as
checking version of /usr/bin/as... 2.27, ok
checking for /usr/bin/ld... /usr/bin/ld
checking version of /usr/bin/ld... 2.27, ok
checking for gnumake... no
checking for gmake... gmake
checking version of gmake... 4.2, ok
checking for gnumsgfmt... no
checking for gmsgfmt... no
checking for msgfmt... msgfmt
checking version of msgfmt... 0.19.8.1, ok
checking for makeinfo... no
checking for sed... sed
checking version of sed... 4.2.2, ok
checking for gawk... gawk
checking version of gawk... 4.0.2, ok
checking for bison... bison
checking version of bison... 3.0.4, ok
checking if gcc -B/usr/bin/ is sufficient to build libc... no
checking for nm... nm
checking for python3... no
checking for python... python
configure: error:
*** These critical programs are missing or too old: compiler
*** Check the INSTALL file for required versions.
出现报错,gcc版本太旧,升级gcc
[root@wenzi build]# yum -y install centos-release-scl
[root@wenzi build]# yum -y install devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
[root@wenzi build]# scl enable devtoolset-8 bash
[root@wenzi build]# source /opt/rh/devtoolset-8/enable
[root@wenzi build]# echo "source /opt/rh/devtoolset-8/enable" >>/etc/profile
[root@wenzi build]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-8.3.1-20190311/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
再次编译安装glibc2.28
[root@wenzi build]# ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
[root@wenzi build]# make
[root@wenzi build]# make install
...
LD_SO=ld-linux-x86-64.so.2 CC="gcc -B/usr/bin/" /usr/bin/perl scripts/test-installation.pl /usr/local/glibc-2.28/build/
/usr/bin/ld: cannot find -lnss_test2
collect2: error: ld returned 1 exit status
Execution of gcc -B/usr/bin/ failed!
The script has found some problems with your installation!
Please read the FAQ and the README file and check the following:
- Did you change the gcc specs file (necessary after upgrading from
Linux libc5)?
- Are there any symbolic links of the form libXXX.so to old libraries?
Links like libm.so -> libm.so.5 (where libm.so.5 is an old library) are wrong,
libm.so should point to the newly installed glibc file - and there should be
only one such link (check e.g. /lib and /usr/lib)
You should restart this script from your build directory after you've
fixed all problems!
Btw. the script doesn't work if you're installing GNU libc not as your
primary library!
make[1]: *** [Makefile:111: install] Error 1
make[1]: Leaving directory '/usr/local/glibc-2.28'
make: *** [Makefile:12: install] Error 2
出现报错,未能解决,但查到有人说报错可以无视。CentOS 7.6 编译安装最新版本glibc2.30 实录-阿里云开发者社区
继续MySQL初始化
[root@wenzi build]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.11' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.22' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by mysqld)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
mysqld: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
mysqld: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/../lib/private/libprotobuf-lite.so.3.19.4)
出现报错,但关于glibc版本的错误消失了。
第一行错误通常是由于两个不兼容的库版本导致的。CXXABI_1.3.11是libstdc++.so.6库中的一个符号版本。当程序在运行时,动态链接器会尝试寻找并加载符号版本所对应的库。如果找不到或者找到的版本不匹配,就会出现链接错误。
错误信息中 CXXABI相关最高版本为CXXABI_1.3.11
查看本机有无对应版本,本机CXXABI最高为1.3.7
[root@wenzi ~]# strings /usr/lib64/libstdc++.so.6 | grep CXXABI
CXXABI_1.3
CXXABI_1.3.1
CXXABI_1.3.2
CXXABI_1.3.3
CXXABI_1.3.4
CXXABI_1.3.5
CXXABI_1.3.6
CXXABI_1.3.7
CXXABI_TM_1
查询libstdc++.so.6本机位置
[root@wenzi ~]# find / -name libstdc++.so.6* #ls -l libstdc++.so*
/usr/lib64/libstdc++.so.6
/usr/lib64/libstdc++.so.6.0.19
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.py
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyc
/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.19-gdb.pyo
安装包含 CXXABI_1.3.11 的 libstdc++.so 包,但是能找到最新版的包 libstdc++-4.8.5-44.el7.x86_64.rpm,地址https://centos.pkgs.org/7/centos-x86_64/libstdc++-4.8.5-44.el7.x86_64.rpm.html
却不包含CXXABI_1.3.11
通过安装 Anaconda3 ,里面这个libstdc++.so高版本的包可以直接用。
[root@wenzi build]# mkdir /usr/local/anaconda
[root@wenzi build]# cd /usr/local/anaconda
[root@wenzi anaconda]# curl -O https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh
[root@wenzi anaconda]# ls
Anaconda3-2022.10-Linux-x86_64.sh
[root@wenzi anaconda]# chmod +x Anaconda3-2022.10-Linux-x86_64.sh
[root@wenzi anaconda]# ./Anaconda3-2022.10-Linux-x86_64.sh
此时显示Anaconda的信息,按enter
并且会出现More,继续按Enter,出现下图输入yes
然后会提示是否修改安装路径,不修改继续回车,最终出现下图,完成安装
验证
[root@wenzi anaconda]# vim /etc/profile.d/anaconda3.sh
export PATH="/root/anaconda3/bin:$PATH"
[root@wenzi anaconda]# source /etc/profile.d/anaconda3.sh
[root@wenzi anaconda]# python3
Python 3.9.13 (main, Aug 25 2022, 23:26:10)
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
KeyboardInterrupt
>>> quit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>>
#输出 Ctrl -D 回到centos命令行
[root@wenzi anaconda]# conda -V
conda 22.9.0
查找anaconda3中的libstdc++.so相关包
[root@wenzi ~]# find / -name libstdc++.so.6
/root/anaconda3/pkgs/libstdcxx-ng-11.2.0-h1234567_1/lib/libstdc++.so.6
/root/anaconda3/lib/libstdc++.so.6
/usr/lib64/libstdc++.so.6
[root@wenzi ~]# ll /root/anaconda3/lib/libstdc*
lrwxrwxrwx 1 root root 19 Sep 6 17:23 /root/anaconda3/lib/libstdc++.so -> libstdc++.so.6.0.29
lrwxrwxrwx 1 root root 19 Sep 6 17:23 /root/anaconda3/lib/libstdc++.so.6 -> libstdc++.so.6.0.29
-rwxrwxr-x 2 root root 17981480 Jun 1 2022 /root/anaconda3/lib/libstdc++.so.6.0.29
查看有无对应 CXXABI_1.3.11 版本,有CXXABI_1.3.11
[root@wenzi ~]# strings /root/anaconda3/lib/libstdc++.so.6.0.29 | grep CXXABI
CXXABI_1.3
CXXABI_1.3.1
CXXABI_1.3.2
CXXABI_1.3.3
CXXABI_1.3.4
CXXABI_1.3.5
CXXABI_1.3.6
CXXABI_1.3.7
CXXABI_1.3.8
CXXABI_1.3.9
CXXABI_1.3.10
CXXABI_1.3.11
CXXABI_1.3.12
CXXABI_1.3.13
CXXABI_TM_1
CXXABI_FLOAT128
CXXABI_1.3
CXXABI_1.3.11
CXXABI_1.3.2
CXXABI_1.3.6
CXXABI_FLOAT128
CXXABI_1.3.12
CXXABI_1.3.9
CXXABI_1.3.1
CXXABI_1.3.5
CXXABI_1.3.8
CXXABI_1.3.13
CXXABI_1.3.4
CXXABI_TM_1
CXXABI_1.3.7
CXXABI_1.3.10
CXXABI_1.3.3
将anaconda3的libstdc++.so.6.0.29复制到/lib64下,删除原有软链接,新建软链接
[root@wenzi ~]# cp /root/anaconda3/lib/libstdc++.so.6.0.29 /lib64/
[root@wenzi ~]# rm -f /lib64/libstdc++.so.6
[root@wenzi ~]# ln -s /lib64/libstdc++.so.6.0.29 /lib64/libstdc++.so.6
[root@wenzi ~]# ll /lib64/libstdc++.so.6*
lrwxrwxrwx 1 root root 26 Sep 6 17:49 /lib64/libstdc++.so.6 -> /lib64/libstdc++.so.6.0.29
-rwxr-xr-x. 1 root root 995840 Sep 30 2020 /lib64/libstdc++.so.6.0.19
-rwxr-xr-x 1 root root 17981480 Sep 6 17:48 /lib64/libstdc++.so.6.0.29
继续MySQL初始化,成功。最后一行末尾是默认密码
[root@wenzi ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
2023-09-06T10:38:09.775461Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.34-linux-glibc2.28-x86_64/bin/mysqld (mysqld 8.0.34) initializing of server in progress as process 40075
2023-09-06T10:38:09.792484Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-09-06T10:38:10.583537Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-09-06T10:38:12.286737Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 25zsG**(i3XL
[root@wenzi ~]# mysqld_safe --user &
[root@wenzi ~]# cp support-files/mysql.server /etc/init.d/mysql.server
#可通过/etc/init.d/mysql.server start 或 status 或 stop 控制mysql
[root@wenzi ~]# /etc/init.d/mysql.server status
SUCCESS! MySQL running (2613)
[root@wenzi ~]# mysql -uroot -p
mysql: error while loading shared libraries: libncurses.so.6: cannot open shared object file: No such file or directory
初次登录数据库时出错,缺少 libncurses.so.6
查询本机 libncurses.so.6 文件,可见/usr/lib64目录下无libncurses.so.6,anaconda3里有一个libncurses.so.6 ,将其复制到 /usr/lib64目录下
[root@wenzi ~]# find / -name libncurses.so*
/root/anaconda3/pkgs/ncurses-6.3-h5eee18b_3/lib/libncurses.so.6
/root/anaconda3/pkgs/ncurses-6.3-h5eee18b_3/lib/libncurses.so.6.3
/root/anaconda3/pkgs/ncurses-6.3-h5eee18b_3/lib/libncurses.so
/root/anaconda3/lib/libncurses.so
/root/anaconda3/lib/libncurses.so.6
/root/anaconda3/lib/libncurses.so.6.3
/usr/lib64/libncurses.so.5
/usr/lib64/libncurses.so.5.9
[root@wenzi ~]# cp /root/anaconda3/lib/libncurses.so.6 /usr/lib64/
[root@wenzi ~]# mysql -uroot -p
mysql: error while loading shared libraries: libtinfo.so.6: cannot open shared object file: No such file or directory
出现错误,处理,道理同上;成功登录数据库
[root@wenzi ~]# find / -name libtinfo.so*
/root/anaconda3/pkgs/ncurses-6.3-h5eee18b_3/lib/libtinfo.so.6
/root/anaconda3/pkgs/ncurses-6.3-h5eee18b_3/lib/libtinfo.so.6.3
/root/anaconda3/pkgs/ncurses-6.3-h5eee18b_3/lib/libtinfo.so
/root/anaconda3/lib/libtinfo.so
/root/anaconda3/lib/libtinfo.so.6
/root/anaconda3/lib/libtinfo.so.6.3
/usr/lib64/libtinfo.so.5
/usr/lib64/libtinfo.so.5.9
[root@wenzi ~]# cp /root/anaconda3/lib/libtinfo.so.6 /usr/lib64/
[root@wenzi ~]# mysql -uroot -p'25zsG**(i3XL'
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.34
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin.123';
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[root@wenzi ~]# mysql -uroot -p'Admin.123'
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 9
Server version: 8.0.34 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit
Bye
源码编译安装
MySQL :: MySQL 8.0 Reference Manual :: 2.8.7 MySQL Source-Configuration Options 官方文档
[root@wenzi ~]# ls
anaconda-ks.cfg mysql-boost-8.0.32.tar.gz original-ks.cfg
#准备依赖
[root@wenzi ~]# yum -y install epel-release && yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake libudev-devel
[root@wenzi ~]# tar -zx -f mysql-boost-8.0.32.tar.gz -C /usr/local/
[root@wenzi ~]# ln -s /usr/local/mysql-8.0.32/ /usr/local/mysql
[root@wenzi ~]# cd /usr/local/mysql
[root@wenzi mysql]# ls
boost components Doxyfile-ignored include libchangestreams man mysys router sql support-files vio
client config.h.cmake Doxyfile.in INSTALL libmysql mysql packaging run_doxygen.cmake sql-common testclients
cmake configure.cmake doxygen_resources libbinlogevents libservices mysql-test plugin scripts storage unittest
CMakeLists.txt Docs extra libbinlogstandalone LICENSE MYSQL_VERSION README share strings utilities
[root@wenzi mysql]# cmake . \
-DWITH_BOOST=/usr/local/bootst \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1
-- Running cmake version 2.8.12.2
CMake Warning at CMakeLists.txt:82 (MESSAGE):
Please use cmake3 rather than cmake on this platform
-- Please install cmake3 (yum install cmake3)
CMake Error at CMakeLists.txt:112 (CMAKE_MINIMUM_REQUIRED):
CMake 3.5.1 or higher is required. You are running version 2.8.12.2
-- Configuring incomplete, errors occurred!
出现报错,CMake版本太低,需要CMake 3.5.1或更高版本
下载make,地址 Index of /files
[root@wenzi ~]# curl -O https://cmake.org/files/v3.7/cmake-3.7.2.tar.gz
[root@wenzi ~]# tar -xz -f cmake-3.7.2.tar.gz -C /usr/local/
[root@wenzi ~]# cd /usr/local/cmake-3.7.2/
[root@wenzi cmake-3.7.2]# ls
Auxiliary CMakeCPackOptions.cmake.in CMakeLogo.gif configure CTestConfig.cmake doxygen.config Modules Source Utilities
bootstrap CMakeGraphVizOptions.cmake cmake_uninstall.cmake.in CONTRIBUTING.rst CTestCustom.cmake.in Help Packaging Templates
CMakeCPack.cmake CMakeLists.txt CompileFlags.cmake Copyright.txt DartConfig.cmake Licenses README.rst Tests
[root@wenzi cmake-3.7.2]# ./configure && make && make install
#编译安装后发现版本没变化,需要重启
[root@wenzi cmake-3.7.2]# reboot
[root@wenzi ~]# cmake --version
cmake version 3.7.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
继续配置mysql
[root@wenzi mysql]# cmake . \
-DWITH_BOOST=/usr/local/mysql/boost \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1
-- Running cmake version 3.7.2
-- Could NOT find Git (missing: GIT_EXECUTABLE)
-- This is .el7. as found from 'rpm -qf /'
-- Looking for a devtoolset compiler
CMake Warning at CMakeLists.txt:392 (MESSAGE):
Could not find devtoolset compiler/linker in /opt/rh/devtoolset-11
CMake Warning at CMakeLists.txt:394 (MESSAGE):
You need to install the required packages:
yum install devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils
CMake Error at CMakeLists.txt:396 (MESSAGE):
Or you can set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER explicitly.
-- Configuring incomplete, errors occurred!
出现错误,缺少文件,下载相关文件;再次cmake会出现报错,根据报错提示cmake时添加-DFORCE_INSOURCE_BUILD=1 ,再次cmake
[root@wenzi mysql]# yum -y install git && yum -y install centos-release-scl && yum -y install devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils
[root@wenzi mysql]# rm -f CMakeCache.txt
[root@wenzi mysql]# rm -f /etc/my.cnf
[root@wenzi mysql]# cmake . \
-DWITH_BOOST=/usr/local/mysql/boost \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DFORCE_INSOURCE_BUILD=1
编译;安装
[root@wenzi mysql]# make && make install
继续配置MySQL
[root@wenzi ~]# vim /etc/profile.d/mysql80.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@wenzi ~]# source /etc/profile.d/mysql80.sh
[root@wenzi ~]# groupadd mysql && useradd -r -g mysql -s /bin/false mysql && cd /usr/local/mysql/
[root@wenzi mysql]# mkdir mysql-files
[root@wenzi mysql]# chown mysql:mysql mysql-files
[root@wenzi mysql]# chmod 750 mysql-files
[root@wenzi mysql]# chown -R mysql:mysql /usr/local/mysql/
[root@wenzi mysql]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
[root@wenzi mysql]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
2023-09-07T11:40:24.810283Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.32/runtime_output_directory/mysqld (mysqld 8.0.32) initializing of server in progress as process 56935
2023-09-07T11:40:24.812541Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2023-09-07T11:40:24.812546Z 0 [Warning] [MY-013244] [Server] --collation-server: 'utf8mb3_general_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
2023-09-07T11:40:24.817945Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-09-07T11:40:25.047872Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-09-07T11:40:25.611765Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Mwl_rVaxM2Lk
初始化完成,末尾是默认密码;上面出现两个警告,建议使用新的UTF8MB4替代旧的。
[root@wenzi ~]# mysqld_safe --user &
[root@wenzi ~]# cp support-files/mysql.server /etc/init.d/mysql.server
[root@wenzi ~]# chmod +x /etc/init.d/mysql.server
#需要重启,不然 /etc/init.d/mysql.server status 等命令会报错
[root@wenzi ~]# reboot
#启动mysql
[root@wenzi ~]# /etc/init.d/mysql.server start
修改密码
[root@wenzi ~]# /etc/init.d/mysql.server status
SUCCESS! MySQL running (1480)
[root@wenzi ~]# mysql -uroot -p'Mwl_rVaxM2Lk'
mysql> alter user 'root'@'localhost' identified by 'Admin.123';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@wenzi ~]# mysql -uroot -p'Admin.123'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
...
单机多实例
MySQL多实例就是在一台服务器上同时开启多个不同的服务端口(如:3306、3307等),同时运
行多个MySQL服务进程,这些服务进程通过不同的Socket监听不同的服务端口来提供服务。
配置方案 多配置文件+多启动程序
针对每个实例都有独立的配置文件和目录,管理灵活
实验
CentOS8实现mariadb10.3.28的yum安装的多实例
关闭防火墙、selinux
安装mariadb
[root@wenzi ~]$yum -y install mariadb-server
准备三个实例目录
[root@wenzi ~]$mkdir -pv /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}
[root@wenzi ~]$chown -R mysql:mysql /mysql/
[root@wenzi ~]$tree -d /mysql
/mysql
├── 3306
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
├── 3307
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
└── 3308
├── bin
├── data
├── etc
├── log
├── pid
└── socket
21 directories
生成数据库文件
[root@wenzi ~]$mysql_install_db --user=mysql --datadir=/mysql/3306/data
[root@wenzi ~]$mysql_install_db --user=mysql --datadir=/mysql/3307/data
[root@wenzi ~]$mysql_install_db --user=mysql --datadir=/mysql/3308/data
修改各自配置文件
[root@wenzi ~]$vim /mysql/3306/etc/my.cnf
[mysqld]
port=3306
datadir=/mysql/3306/data
socket=/mysql/3306/socket/mysql.sock
log-error=/mysql/3306/log/mysql.log
pid-file=/mysql/3306/pid/mysql.pid
[root@wenzi ~]$sed 's/3306/3307/' /mysql/3306/etc/my.cnf > /mysql/3307/etc/my.cnf
[root@wenzi ~]$sed 's/3306/3308/' /mysql/3306/etc/my.cnf > /mysql/3308/etc/my.cnf
准备启动脚本,刚初始化完数据库,默认是没有密码,后面需要修改密码
[root@wenzi ~]$vim /mysql/3306/bin/mysqld
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
# ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
${cmd_path}/mysqladmin -u ${mysql_user} -S ${mysql_sock} shutdown
fi
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac
[root@wenzi ~]$sed 's/3306/3307/' /mysql/3306/bin/mysqld > /mysql/3307/bin/mysqld
[root@wenzi ~]$sed 's/3306/3308/' /mysql/3306/bin/mysqld > /mysql/3308/bin/mysqld
[root@wenzi ~]$chmod +x /mysql/3306/bin/mysqld
[root@wenzi ~]$chmod +x /mysql/3307/bin/mysqld
[root@wenzi ~]$chmod +x /mysql/3308/bin/mysqld
测试
[root@wenzi ~]$/mysql/3308/bin/mysqld start
[root@wenzi ~]$/mysql/3307/bin/mysqld start
[root@wenzi ~]$/mysql/3306/bin/mysqld start
[root@wenzi ~]$ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
登录实例,查看当前使用的端口,以3306为例
[root@wenzi ~]$mysql -h127.0.0.1 -P3306
...
MariaDB [(none)]> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.000 sec)
修改密码,并修改启动脚本中密码;此过程重复三次
[root@wenzi ~]$mysqladmin -uroot -S /mysql/3306/socket/mysql.sock password 'Admin.'
[root@wenzi ~]$vim /mysql/3306/bin/mysqld
#!/bin/bash
...
mysql_pwd="Admin."
...
function_stop_mysql()
{
...
${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
# ${cmd_path}/mysqladmin -u ${mysql_user} -S ${mysql_sock} shutdown
fi
}
...
此时再登录需要输入密码,以3306为例
[root@wenzi ~]$mysql -uroot -pAdmin. -h127.0.0.1 -P3306
开机启动
[root@wenzi ~]$vim /etc/rc.d/rc.local
...
for i in {3306..3308};do /mysql/$i/bin/mysqld start;done
[root@wenzi ~]$chmod +x /etc/rc.d/rc.local
重启验证