Linux中二进制方式安装MySQL5.7
- 安装
- 安装方式
- 官网下载安装包
- 创建用户组mysql,用户和目录
- 把下载的安装包,放到/home/mysql/
- 将本地文件拷贝到远程: scp 文件名 –用户名@计算机IP或者计算机名称:远程路径
- 验证包
- 解压安装包,移动到/usr/local/mysql/
- 在根下创建MySQL目录,改归属权限
- 修改内核参数、环境变量、安装libaio库
- 修改内核参数
- 环境变量
- 配置参数文件 my.cnf
- 初始化
- 启动
- 修改密码
- 库、表管理
- 默认系统库
- 库的管理
- 表的管理
- 表管理命令:
安装
安装方式
源码安装
RPM包安装
二进制安装
官网下载安装包
网址:https://www.mysql.com/downloads/
直接点击下载
创建用户组mysql,用户和目录
[root@control ~]# groupadd mysql
[root@control ~]# useradd -g mysql mysql
[root@control ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@control ~]#
把下载的安装包,放到/home/mysql/
将本地文件拷贝到远程: scp 文件名 –用户名@计算机IP或者计算机名称:远程路径
[root@server1 ~]# scp mysql-5.7.42-linux-glibc2.12-x86_64.tar root@172.25.254.100:/home/mysql/
mysql-5.7.42-linux-glibc2.12-x86_64.tar 100% 680MB 3.5MB/s 03:15
[root@server1 ~]#
验证包
[root@control ~]# ls /home/mysql/ -al
total 696220
drwx------. 2 mysql mysql 130 May 6 12:10 .
drwxr-xr-x. 4 root root 32 May 6 11:16 ..
-rw-r--r--. 1 mysql mysql 312 Aug 30 2019 .bashrc
-rw-r--r--. 1 root root 712912384 May 6 12:13 mysql-5.7.42-linux-glibc2.12-x86_64.tar
解压安装包,移动到/usr/local/mysql/
[root@control local]# mkdir -p /usr/local/mysql/
[root@control local]# ls /home/mysql/
mysql-5.7.42-linux-glibc2.12-x86_64.tar
[root@control local]# mv /home/mysql/mysql-5.7.42-linux-glibc2.12-x86_64.tar /usr/local/mysql/
[root@control local]# ls /usr/local/mysql/
mysql-5.7.42-linux-glibc2.12-x86_64.tar
[root@control local]# tar -xvf /usr/local/mysql/mysql-5.7.42-linux-glibc2.12-x86_64.tar
mysql-test-5.7.42-linux-glibc2.12-x86_64.tar.gz
mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
[root@control mysql]# tar -xvf mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
[root@control mysql]# ls -l
total 1392408
drwxr-xr-x. 9 root root 129 May 6 13:42 mysql-5.7.42-linux-glibc2.12-x86_64
-rwxr-xr-x. 1 mysql mysql 712912384 May 6 12:21 mysql-5.7.42-linux-glibc2.12-x86_64.tar
-rw-r--r--. 1 7161 31415 678258106 Mar 17 00:18 mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
-rw-r--r--. 1 7161 31415 34651721 Mar 17 00:16 mysql-test-5.7.42-linux-glibc2.12-x86_64.tar.gz
改权限、归属
[root@control mysql]# chmod -R 755 /usr/local/mysql
[root@control mysql]# chown -R mysql:mysql /usr/local/mysql
[root@control mysql]# ls -al
total 1392408
drwxr-xr-x. 3 mysql mysql 201 May 6 13:40 .
drwxr-xr-x. 13 root root 249 May 6 12:24 ..
drwxr-xr-x. 9 mysql mysql 129 May 6 13:42 mysql-5.7.42-linux-glibc2.12-x86_64
-rwxr-xr-x. 1 mysql mysql 712912384 May 6 12:21 mysql-5.7.42-linux-glibc2.12-x86_64.tar
-rwxr-xr-x. 1 mysql mysql 678258106 Mar 17 00:18 mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
-rwxr-xr-x. 1 mysql mysql 34651721 Mar 17 00:16 mysql-test-5.7.42-linux-glibc2.12-x86_64.tar.gz
在根下创建MySQL目录,改归属权限
[root@control bin]# mkdir -p /mysql/3306/{data,redo,binlog,tmp,logs}
[root@control bin]# chown -R mysql:mysql /mysql/
[root@control bin]# chmod -R 755 /mysql/
[root@control bin]# ls / -al
total 20
dr-xr-xr-x. 18 root root 237 May 6 13:47 .
dr-xr-xr-x. 18 root root 237 May 6 13:47 ..
lrwxrwxrwx. 1 root root 7 Aug 12 2018 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Oct 30 2020 boot
drwxr-xr-x. 21 root root 3180 May 6 10:45 dev
drwxr-xr-x. 82 root root 8192 May 6 12:02 etc
drwxr-xr-x. 4 root root 32 May 6 11:16 home
lrwxrwxrwx. 1 root root 7 Aug 12 2018 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Aug 12 2018 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Aug 12 2018 media
drwxr-xr-x. 2 root root 6 Aug 12 2018 mnt
drwxr-xr-x. 3 mysql mysql 18 May 6 13:47 mysql
修改内核参数、环境变量、安装libaio库
修改内核参数
[root@control bin]# vim /etc/security/limits.conf
mysql soft nproc 65535
mysql hard nproc 65535
mysql soft nofile 65535
mysql hard nofile 65535
:wq
[root@control bin]# yum install libaio
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 1:13:42 ago on Sat 06 May 2023 01:57:46 PM CST.
Package libaio-0.3.112-1.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@control bin]# yum list|grep libaio
libaio.x86_64 0.3.112-1.el8 @anaconda
libaio.i686 0.3.112-1.el8 baseos
libaio-devel.i686 0.3.112-1.el8 baseos
libaio-devel.x86_64 0.3.112-1.el8 baseos
[root@control bin]#
环境变量
[mysql@control ~]$ vim .bash_profile
[mysql@control ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PATH=/usr/local/mysql/bin:$PATH
[mysql@control ~]$
找到PATH行,看到有usr/local/mysql/bin:,就成功了
[mysql@control ~]$ . ./.bash_profile
[mysql@control ~]$ env
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOSTNAME=control
USER=mysql
PWD=/home/mysql
HOME=/home/mysql
MAIL=/var/spool/mail/mysql
SHELL=/bin/bash
TERM=xterm
SHLVL=1
LOGNAME=mysql
PATH=/usr/local/mysql/bin:/home/mysql/.local/bin:/home/mysql/bin:/home/mysql/.local/bin:/home/mysql/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
配置参数文件 my.cnf
在 /mysql,下创建配置文件
[root@control 3306]# vim my.cnf
[root@control 3306]# pwd
/mysql/3306
[root@control 3306]# ls
binlog data logs my.cnf redo tmp
[root@control 3306]#
[client]
default-character-set = utf8
port = 3306
socket = /mysql/3306/tmp/mysql.sock
[mysqld]
port = 3306
character_set_server = utf8
server-id = 1
socket = /mysql/3306/tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /mysql/3306/data
tmpdir = /mysql/3306/tmp
log-error = /mysql/3306/logs/alert_3306.log
pid-file = /mysql/3306/tmp/mysql.pid
log-bin = /mysql/3306/binlog/mysql-bin
slow_query_log_file = /mysql/3306/logs/slow.log
default-storage-engine = INNODB
innodb_data_file_path = ibdata1:2G:autoextend
innodb_log_group_home_dir = /mysql/3306/redo
innodb_file_per_table = 1
~
~
~
~
~
~
~
~
:wq
初始化
启动
修改密码
库、表管理
默认系统库
information_schema:记录了用户、表、视图等元数据信息。这个库是虚拟出来的库,是由MySQL实例构建和维护的,其对象都保存在内存中,在磁盘上找不到对应的物理存在。用户也无法在该库下创建对象,只能查询。
mysql:记录了用户权限、帮助、日志等信息。
performance_schema:记录了数据库性能原始指标等信息。
sys:sys库提供了一些访问performance_schema的视图,目标是把performance_schema的复杂度降低,方便DBA快速了解数据的状态
库的管理
创建/删除/查看库
- 查看建库语法: help create database;
- 创建数据库: create database bddb;
- 删除数据库: drop database bddb;
- 查看当前存在的库: show databases;
- 查看创建库脚本: show create database bddb;
例:
mysql> create database dbaaa;
Query OK, 1 row affected (1.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| dbaaa |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show create database dbaaa;
+----------+---------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------------------------------------------------------------------+
| dbaaa | CREATE DATABASE `dbaaa` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+---------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.05 sec)
表的管理
查看建表语法:help create table;
查看建表语句:show create table xxxx; o
查看表结构信息: show columns from xxxx 或desc xxxx;
查看索引信息 : show index from mysql.db;
查看修改表的语法 : help alter table;
添加列 : alter table users add (email varchar(50),salary smallint);
删除列 :alter table users drop salary;
修改列长 : alter table user modify username varchar(20);
表管理命令:
建表:表用于存储数据文件
create table 库名.表名(
字段名1 类型(宽度),
字段名2 类型(宽度),
... ...
) DEFAULT CHARSET=utf8;
##指定中文字符,让表支持插入中文
desc 库名.表名; ##查看表结构
drop table 库名.表名; ##删除表
select * from 库名.表名; ##查看表记录
insert into 库名.表名 values(值列表); ##插入表记录
update 库名.表名 set 字段=值; ##修改表记录
delete from 库名.表名; ##删除表记录
show create table 表名; ##查看创建表的命令和相关属性