MYSQL数年库安装

MySQL系列
 MySQL 的三大主要分支
mysql
mariadb
percona Server

MySQL系列
2.2.2.1 MySQL 的三大主要分支
mysql
mariadb
percona Server

官方文档
https://dev.mysql.com/doc/
https://mariadb.com/kb/en/
https://www.percona.com/software/mysql-database/percona-server
版本演变
MySQL5.1 --> 5.5 --> 5.6 --> 5.7 -->8.0
MariaDB5.5 -->10.0--> 10.1 --> 10.2 --> 10.3 --> 10.4 --> 10.5
MYSQL的特性
插件式存储引擎:也称为表类型,存储管理器有多种实现版本,功能和特性可能均略有差别;用
户可根据需要灵活选择,Mysql5.5.5开始innoDB引擎是MYSQL默认引擎
MyISAM ==> Aria
InnoDB ==> XtraDB
单进程,多线程
诸多扩展和新特性
提供了较多测试组件
开源

RPM包安装MySQL
CentOS 安装光盘
项目官方:https://downloads.mariadb.org/mariadb/repositories/
国内镜像:https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/
CentOS 8:安装光盘直接提供
mysql-server8.0
mariadb-server : 10.3.17
CentOS 7:安装光盘直接提供
mariadb-server5.5 服务器包

mariadb
CentOS 6
客户端工具包
mysql-server5.1 服务器包
mysql 客户端工具包

 

初始化脚本提高安全性

运行脚本:mysql_secure_installation

设置数据库管理员root口令
禁止root远程登录
删除anonymous用户帐号
删除test数据库

MySQL 组成

客户端程序
设置数据库管理员root口令
禁止root远程登录
删除anonymous用户帐号
删除test数据库
mysql: 交互式的CLI工具
mysqldump:备份工具,基于mysql协议向mysqld发起查询请求,并将查得的所有数据转换成
insert等写操作语句保存文本文件中
mysqladmin:基于mysql协议管理mysqld
mysqlimport:数据导入工具
MyISAM存储引擎的管理工具:
myisamchk:检查MyISAM
myisampack:打包MyISAM表,只读

服务器端程序

mysqld_safemysqld

mysqld_multi 多实例 ,

示例:mysqld_multi --example

用户账号
mysql用户账号由两部分组成:
'USERNAME'@'HOST
说明:
HOST限制此用户可通过哪些远程主机连接mysql服务器
支持使用通配符:
% 匹配任意长度的任意字符
172.16.0.0/255.255.0.0 172.16.%.%
_ 匹配任意单个字符

mysql 客户端命令
 mysql 运行命令类型
客户端命令:本地执行,每个命令都完整形式和简写格式
服务端命令:通过mysql协议发往服务器执行并取回结果,命令末尾都必须使用命令结束符号,默
认为分号
 mysql 使用模式
交互模式
脚本模式:
mysql> h, help
mysql> uuse
mysql> sstatus
mysql> !system

mysql -uUSERNAME -pPASSWORD < /path/somefile.sql
cat /path/somefile.sql | mysql -uUSERNAME -pPASSWORD
mysql>source /path/from/somefile.sql
mysql命令使用格式
mysql客户端常用选项:
登录系统:
运行mysql命令:
范例:mysql的配置文件,修改提示符
mysql -uUSERNAME -pPASSWORD < /path/somefile.sql
cat /path/somefile.sql | mysql -uUSERNAME -pPASSWORD
mysql>source /path/from/somefile.sql
mysql [OPTIONS] [database]
-A, --no-auto-rehash 禁止补全

-u, --user=
-h, --host=
用户名,默认为root
服务器主机,默认为localhost
-p, --passowrd= 用户密码,建议使用-p,默认为空密码
-P, --port= 服务器端口
-S, --socket=
-D, --database=
-C, --compress
指定连接socket文件路径
指定默认数据库
启用压缩
-e “SQL“ 执行SQL命令
-V, --version
-v --verbose
--print-defaults
显示版本
显示详细信息
获取程序默认使用的配置

 

运行mysql命令
mysql>use mysql
mysql>select user(); #查看当前用户
mysql>SELECT User,Host,Password FROM user;

MYSQL配置文件修命令提示符

查看mysql版本
[root@centos8 ~]#mysql -V
mysql Ver 15.1 Distrib 10.3.11-MariaDB, for Linux (x86_64) using readline 5.1
#临时修改mysql提示符
[root@centos8 ~]#mysql -uroot -pcentos --prompt="\r:\m:\s(\u@\h) [\d]>\_"
#临时修改mysql提示符
[root@centos8 ~]#export MYSQL_PS1="\r:\m:\s(\u@\h) [\d]>\_"
#持久修改mysql提示符
[root@centos8 ~]#vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
prompt="\r:\m:\s(\u@\h) [\d]>\_"

mysql --print-defaults -v
mysql would have been started with the following arguments:
--prompt=
:m:s(u@h) [d]>\_ -v
[root@centos8 ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 11
Server version: 10.3.11-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
10:29:30(root@localhost) [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
10:29:34(root@localhost) [mysql]> exit

  

[root@centos8 ~]#cat /etc/my.cnf.d/mysql-clients.cnf
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
[mysql]
prompt="\r:\m:\s> "
user=root
password=centos

 配置客户端mysql的自动登录

vim/etc/my.cnf.d/client.conf
[client]
user=root
password=centos

  mysqladmin命令

mysqladmin 命令格式
范例:
[root@centos8 ~]#mysql --print-defaults -v
mysql would have been started with the following arguments:
--prompt=
:m:s(u@h) [d]>\_ -v
[root@centos8 ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 11
Server version: 10.3.11-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
10:29:30(root@localhost) [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
10:29:34(root@localhost) [mysql]> exit
[root@centos8 ~]#cat /etc/my.cnf.d/mysql-clients.cnf
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
[mysql]
prompt="\r:\m:\s> "
user=root
password=centos
vim/etc/my.cnf.d/client.conf
[client]
user=wang
password=centos
mysqladmin [OPTIONS] command command....2.4.4.5 服务器端配置
服务器端(mysqld):工作特性有多种配置方式
1、命令行选项:
2、配置文件:类ini格式,集中式的配置,能够为mysql的各应用程序提供配置信息
服务器端配置文件:
配置文件格式:
[mysqld]
[mysqld_safe]
[mysqld_multi]
[mysql]
[mysqldump]
[server]
[client]
格式:parameter = value
说明:_和- 相同
1,ON,TRUE意义相同, 0,OFF,FALSE意义相同
2.4.4.6 socket地址
服务器监听的两种socket地址:
ip socket: 监听在tcp的3306端口,支持远程通信 ,侦听3306/tcp端口可以在绑定有一个或全部接
口IP上
unix sock: 监听在sock文件上,仅支持本机通信, 如:/var/lib/mysql/mysql.sock)
说明:host为localhost 时自动使用unix sock
2.4.4.7 关闭mysqld网络连接
只侦听本地客户端, 所有客户端和服务器的交互都通过一个socket文件实现,socket的配置存放
在/var/lib/mysql/mysql.sock) 可在/etc/my.cnf修改
范例:
#查看mysql服务是否正常,如果正常提示mysqld is alive
mysqladmin -uroot -pcentos ping
#关闭mysql服务,但mysqladmin命令无法开启
mysqladmin -uroot -pcentos shutdown
#创建数据库testdb
mysqladmin -uroot -pcentos create testdb
#删除数据库testdb
mysqladmin -uroot -pcentos drop testdb
#修改root密码
mysqladmin -uroot -pcentos password magedu’
#日志滚动,生成新文件/var/lib/mysql/mariadb-bin.00000N
mysqladmin -uroot -pcentos flush-logs


mysqladmin 命令格式
范例:
[root@centos8 ~]#mysql --print-defaults -v
mysql would have been started with the following arguments:
--prompt= :m:s(u@h) [d]>\_ -v
[root@centos8 ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 11
Server version: 10.3.11-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
10:29:30(root@localhost) [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
10:29:34(root@localhost) [mysql]> exit
[root@centos8 ~]#cat /etc/my.cnf.d/mysql-clients.cnf
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
[mysql]
prompt="\r:\m:\s> "
user=root
password=centos
vim/etc/my.cnf.d/client.conf
[client]
user=wang
password=centos
mysqladmin [OPTIONS] command command....


服务器端配置

服务器端(mysqld):工作特性有多种配置方式
1、命令行选项:
2、配置文件:类ini格式,集中式的配置,能够为mysql的各应用程序提供配置信息
服务器端配置文件:
配置文件格式:
[mysqld]
[mysqld_safe]
[mysqld_multi]
[mysql]
[mysqldump]
[server]
[client]
格式:parameter = value
说明:_- 相同
1ONTRUE意义相同, 0OFFFALSE意义相同
2.4.4.6 socket地址
服务器监听的两种socket地址:
ip socket: 监听在tcp3306端口,支持远程通信 ,侦听3306/tcp端口可以在绑定有一个或全部接
IP
unix sock: 监听在sock文件上,仅支持本机通信, 如:/var/lib/mysql/mysql.sock)
说明:hostlocalhost 时自动使用unix sock
2.4.4.7 关闭mysqld网络连接
只侦听本地客户端, 所有客户端和服务器的交互都通过一个socket文件实现,socket的配置存放
/var/lib/mysql/mysql.sock) 可在/etc/my.cnf修改
范例:
#查看mysql服务是否正常,如果正常提示mysqld is alive
mysqladmin -uroot -pcentos ping
#关闭mysql服务,但mysqladmin命令无法开启
mysqladmin -uroot -pcentos shutdown
#创建数据库testdb

mysqladmin -uroot -pcentos
#删除数据库testdb
mysqladmin -uroot -pcentos
#修改root密码
create testdb
drop testdb

mysqladmin -uroot -pcentos password magedu’
#日志滚动,生成新文件/var/lib/mysql/mariadb-bin.00000N
mysqladmin -uroot -pcentos flush-logs


服务器端配置
服务器端(mysqld):工作特性有多种配置方式
1、命令行选项:
2、配置文件:类ini格式,集中式的配置,能够为mysql的各应用程序提供配置信息
服务器端配置文件:
配置文件格式:
[mysqld]
[mysqld_safe]
[mysqld_multi]
[mysql]
[mysqldump]
[server]
[client]
格式:parameter = value
说明:_和- 相同
1,ON,TRUE意义相同, 0,OFF,FALSE意义相同
2.4.4.6 socket地址
服务器监听的两种socket地址:
ip socket: 监听在tcp的3306端口,支持远程通信 ,侦听3306/tcp端口可以在绑定有一个或全部接
口IP上
unix sock: 监听在sock文件上,仅支持本机通信, 如:/var/lib/mysql/mysql.sock)
说明:host为localhost 时自动使用unix sock
2.4.4.7 关闭mysqld网络连接
只侦听本地客户端, 所有客户端和服务器的交互都通过一个socket文件实现,socket的配置存放
在/var/lib/mysql/mysql.sock) 可在/etc/my.cnf修改
范例:
#查看mysql服务是否正常,如果正常提示mysqld is alive
mysqladmin -uroot -pcentos ping
#关闭mysql服务,但mysqladmin命令无法开启
mysqladmin -uroot -pcentos shutdown
#创建数据库testdb
mysqladmin -uroot -pcentos create testdb
#删除数据库testdb
mysqladmin -uroot -pcentos drop testdb
#修改root密码
mysqladmin -uroot -pcentos password magedu’
#日志滚动,生成新文件/var/lib/mysql/mariadb-bin.00000N
mysqladmin -uroot -pcentos flush-logs
/etc/my.cnf #Global选项
/etc/mysql/my.cnf #Global选项
~/.my.cnf #User-specific 选项2.5 通用二进制格式安装 MySQL
2.5.1 准备用户
2.5.2 准备数据目录,建议使用逻辑卷
2.5.3 准备二进制程序
2.5.4 准备配置文件
2.5.5 创建数据库文件

vim /etc/my.cnf
[mysqld]
skip-networking=1

 准备用户

roupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -d /data/mysql mysql

 准备数据目录,建议使用逻辑卷

#可选做,后面的脚本mysql_install_db可自动生成此目录
mkdir /data/mysql
chown mysql:mysql /data/mysql

  准备二进制程序

tar xf mariadb-VERSION-linux-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mariadb-VERSION mysql
chown -R root:root /usr/local/mysql/

  准备配置文件

cd /usr/local/mysql
cp -b support-files/my-large.cnf /etc/my.cnf
vim /etc/my.cnf
#mysql语句块中添加以下三个选项
[mysqld]
datadir = /data/mysql
innodb_file_per_table = on #在mariadb5.5以上版的是默认值,可不加
skip_name_resolve = on #禁止主机名解析,建议使用

  创建数据库文件

cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
[root@centos8 mysql]#ls /data/mysql/ -l
total 110604
-rw-rw---- 1 mysql mysql 12582912 Jun 1 16:44 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Jun 1 16:44 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jun 1 16:44 ib_logfile1
drwx------ 2 mysql mysql 4096 Jun 1 16:44 mysql
drwx------ 2 mysql mysql 4096 Jun 1 16:44 performance_schema
drwx------ 2 mysql mysql 4096 Jun 1 16:44 test

  准备服务脚本,并启动服务 

cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
service mysqld start
#如果有对应的service 文件可以执行下面
cp support-files/systemd/mariadb.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl enable --now mariadb

  PATH路径

echo 'PATH=/user/local/mysql/bin:$PATH’ > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh

  安全初始化

/user/local/mysql/bin/mysql_secure_installation

  



原文地址:https://www.cnblogs.com/niewd/p/14345418.html