您现在的位置是:网站首页> 编程资料编程资料
Mysql二进制安装与备份的全过程记录_Mysql_
2023-05-26
437人已围观
简介 Mysql二进制安装与备份的全过程记录_Mysql_
Mysql的二进制安装
下载安装包
[root@localhost mysql]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz --2021-05-04 20:34:21-- https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz Resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14 Connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz [following] --2021-05-04 20:34:23-- https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz Resolving cdn.mysql.com (cdn.mysql.com)... 223.119.236.209 Connecting to cdn.mysql.com (cdn.mysql.com)|223.119.236.209|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 661718255 (631M) [application/x-tar-gz] Saving to: ‘mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz' mysql-5.7.33-linux- 100%[================>] 631.06M 11.3MB/s in 57s 2021-05-04 20:35:21 (11.0 MB/s) - ‘mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz' saved [661718255/661718255]
创建用户
[root@localhost mysql]# useradd -r -M -s /sbin/nologin mysql
解压至指定目录
[root@localhost mysql]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local
创建软链接或者修改目录名
[root@localhost local]# mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql [root@localhost local]# ls apache apr-util etc include lib64 mysql share apr bin games lib libexec sbin src
修改属主和属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql/ [root@localhost local]# ll drwxr-xr-x. 9 mysql mysql 129 五月 4 20:40 mysql
设置环境变量(因为不是用yum装的,找不到mysql程序)
[root@localhost mysql]# vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH [root@localhost mysql]# . /etc/profile.d/mysql.sh [root@localhost mysql]# which mysql /usr/local/mysql/bin/mysql
创建存放数据的目录并修改属主属组
需要一个空间大的目录,或者将目录创建后将硬盘设备挂载在此目录上
[root@localhost mysql]# mkdir /opt/mysql_data [root@localhost mysql]# chown -R mysql.mysql /opt/mysql_data/ [root@localhost mysql]# ll /opt drwxr-xr-x. 2 mysql mysql 6 五月 4 20:58 mysql_data
初始化并保存密码
[root@localhost mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/mysql_data/ //初始化 控制mysql的用户 数据存放目录 2021-05-04T13:01:07.403961Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-05-04T13:01:07.683107Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-05-04T13:01:07.739366Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-05-04T13:01:07.746720Z 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: caa21b8a-acd8-11eb-b7ab-000c294bb269. 2021-05-04T13:01:07.747895Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-05-04T13:01:09.096727Z 0 [Warning] CA certificate ca.pem is self signed. 2021-05-04T13:01:09.485357Z 1 [Note] A temporary password is generated for root@localhost: q_UG8?3sa/l% [root@localhost mysql]# vim password q_UG8?3sa/l%
写配置文件
[root@localhost mysql]# vim /etc/my.cnf [mysqld] basedir = /usr/local/mysql //程序位置 datadir = /opt/mysql_data //数据存放位置 socket = /tmp/mysql.sock //文件套接字位置 port = 3306 //端口 pid-file = /opt/mysql_data/mysql.pid //进程文件位置 user = mysql //用户 skip-name-resolve //跳过域名解析,即直接在内网使用ip连接数据库
配置启动脚本和开机自启
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld [root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/mysql_data#g' /etc/init.d/mysqld [root@localhost mysql]# head -47 /etc/init.d/mysqld |tail -2 basedir=/usr/local/mysql datadir=/opt/mysql_data [root@localhost mysql]# service mysqld start Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'. SUCCESS! [root@localhost mysql]# chkconfig mysqld on [root@localhost mysql]# chkconfig --list Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@localhost mysql]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 *:443 *:* LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22 [::]:*
头文件和库文件配置
[root@localhost mysql]# ln -s /usr/local/mysql/include /usr/include/mysql [root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [root@localhost mysql]# ldconfig //重新读取配置文件
启动并设置密码
[root@localhost local]# mysql -uroot -p mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory //缺少包 [root@localhost local]# dnf provides libncurses.so.5 //查看包所依赖的类库 Warning: failed loading '/etc/yum.repos.d/mssql-server.repo', skipping. ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries Repo : baseos Matched from: Provide : libncurses.so.5 [root@localhost local]# dnf -y install ncurses-compat-libs [root@localhost local]# cat password /sdjtceDy7F7 [root@localhost local]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.33 Copyright (c) 2000, 2021, 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>
Mysql的配置文件
mysql的配置文件为/etc/my.cnf
配置文件查找顺序:若在多个配置文件中均有设定,则最后找到的配置文件中的配置生效
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
| 参数 | 说明 |
|---|---|
| port = 3306 | 设置监听端口,默认3306 |
| socket = /tmp/mysql.sock | 指定套接字文件位置 |
| basedir = /usr/local/mysql | 指定MySQL的安装路径 |
| datadir = /data/mysql | 指定MySQL的数据存放路径 |
| pid-file = /data/mysql/mysql.pid | 指定进程ID文件存放路径 |
| user = mysql | 指定MySQL以什么用户的身份提供服务 |
| skip-name-resolve | 禁止MySQL对外部连接进行DNS解析 使用这一选项可以消除MySQL进行DNS解析的时间 若开启该选项,则所有远程主机连接授权都要使用IP地址方 式否则MySQL将无法正常处理连接请求 |
备份与恢复
数据库常用备份方案
数据库备份方案:
- 全量备份
- 增量备份
- 差异备份
| 备份方案 | 特点 |
|---|---|
| 全量备份 | 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝 数据恢复快,备份时间长 |
| 增量备份 | 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份 与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象 是进行全备后所产生的增加和修改的文件 第二次增量备份的对象是进行第一次增量 备份后所产生的增加和修改的文件,如此类推 没有重复的备份数据 备份时间短 恢复数据时必须按一定的顺序进行 |
| 差异备份 | 备份上一次的完全备份后发生变化的所有文件 差异备份是指在一次全备份后到进行差异备份的这段时间内 对那些增加或者修改文件的备份。在进行恢复时 我们只需对第一次全量备份和最后一次差异备份进行恢复 |
mysql备份工具mysqldump
mysqldump 语法:
mysqldump [OPTIONS] database [tables ...] //备份数据表 mysqldump [OPTIONS] --all-databases [OPTIONS] //备份全部数据库 mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] //备份某个数据库 -uUSERNAME //指定数据库用户名 -hHOST //指定服务器主机,请使用ip地址 -pPASSWORD //指定数据库用户的密码 -P# //指定数据库监听的端口,这里的#需用实际的端口号代替
备份所有数据库
[root@localhost ~]# mysqldump -uroot -p --all-databases > /mysql_dump/all_20210504.sql Enter password: [root@localhost ~]# ls /mysql_dump/ all_20210504.sql
备份某个数据库
[root@localhost ~]# mysqldump -uroot -p --databases hanao > /mysql_dump/hanao_db_20210504.sql Enter password: [root@localhost ~]# ls /mysql_dump/ all_20210504.sql hanao_db_20210504.sql
备份某个数据表
[root@localhost ~]# mysqldump -uroot -p hanao student > /mysql_dump/student_table_20210504.sql Enter password: [root@localhost ~]# ls /mysql_dump/ all_20210504.sql hanao_db_20210504.sql student_table_20210504.sql
mysql全备数据恢复
恢复数据表
mysql> use hanao Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database
点击排行
本栏推荐
