标签 MySQL 下的文章

1、更新/升级软件包存储库

为了确保安装最新的MYSQL,请更新软件包。运行以下命令:

sudo apt update

将已安装的软件包升级到最新版本:

sudo apt upgrade

2、安装MySQL

使用以下命令安装MySQL:

sudo apt install mysql-server

通过以下方面验证安装:

mysqld --version

- 阅读剩余部分 -

MySQLDump是一个通用的实用程序,可用于转储数据库或数据库集合,以进行备份或传输。也可以生成CSV、其他分隔文本或XML格式的文件。

1. 备份数据库

备份指定的数据库到.sql文件:
$ mysqldump -u root -p mydb > mydb2023.sql

2. 恢复数据库

将备份的数据库还原到新的数据库
CREATE DATABASE newdb;
mysql -u root -p password newdb < mydb2023.sql

3. Mysqldump参数说明

--all-databases  , -A                导出全部数据库。
--all-tablespaces  , -Y                导出全部表空间。
--no-tablespaces  , -y                不导出任何表空间信息。
--add-drop-database                每个数据库创建之前添加drop数据库语句。
--add-drop-table                每个数据表创建之前添加drop数据表语句。(默认为打开状态,使用--skip-add-drop-table取消选项)
--add-locks                在每个表导出之前增加LOCK TABLES并且之后UNLOCK  TABLE。(默认为打开状态,使用--skip-add-locks取消选项)
--allow-keywords                允许创建是关键词的列名字。这由表名前缀于每个列名做到。
--apply-slave-statements                在'CHANGE MASTER'前添加'STOP SLAVE',并且在导出的最后添加'START SLAVE'。
--character-sets-dir                字符集文件的目录
--comments                附加注释信息。默认为打开,可以用--skip-comments取消
--compatible                导出的数据将和其它数据库或旧版本的MySQL 相兼容。值可以为ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options、no_field_options等,要使用几个值,用逗号将它们隔开。它并不保证能完全兼容,而是尽量兼容。
--compact                导出更少的输出信息(用于调试)。去掉注释和头尾等结构。可以使用选项:--skip-add-drop-table  --skip-add-locks --skip-comments --skip-disable-keys
--complete-insert,  -c                使用完整的insert语句(包含列名称)。这么做能提高插入效率,但是可能会受到max_allowed_packet参数的影响而导致插入失败。
--compress, -C                在客户端和服务器之间启用压缩传递所有信息
--create-options,  -a                在CREATE TABLE语句中包括所有MySQL特性选项。(默认为打开状态)
--databases,  -B                导出几个数据库。参数后面所有名字参量都被看作数据库名。
--debug                输出debug信息,用于调试。默认值为:d:t,/tmp/mysqldump.trace
--debug-check                检查内存和打开文件使用说明并退出。
--debug-info                输出调试信息并退出
--default-character-set                设置默认字符集,默认值为utf8
--delayed-insert                采用延时插入方式(INSERT DELAYED)导出数据
--delete-master-logs                master备份后删除日志. 这个参数将自动激活--master-data。
--disable-keys                对于每个表,用/*!40000 ALTER TABLE tbl_name DISABLE KEYS */;和/*!40000 ALTER TABLE tbl_name ENABLE KEYS */;语句引用INSERT语句。这样可以更快地导入dump出来的文件,因为它是在插入所有行后创建索引的。该选项只适合MyISAM表,默认为打开状态。
--dump-slave                该选项将主的binlog位置和文件名追加到导出数据的文件中(show slave status)。设置为1时,将会以CHANGE MASTER命令输出到数据文件;设置为2时,会在change前加上注释。该选项将会打开--lock-all-tables,除非--single-transaction被指定。该选项会自动关闭--lock-tables选项。默认值为0。
--master-data                该选项将当前服务器的binlog的位置和文件名追加到输出文件中(show master status)。如果为1,将会输出CHANGE MASTER 命令;如果为2,输出的CHANGE  MASTER命令前添加注释信息。该选项将打开--lock-all-tables 选项,除非--single-transaction也被指定(在这种情况下,全局读锁在开始导出时获得很短的时间;其他内容参考下面的--single-transaction选项)。该选项自动关闭--lock-tables选项。
--events, -E                导出事件。
--extended-insert,  -e                使用具有多个VALUES列的INSERT语法。这样使导出文件更小,并加速导入时的速度。默认为打开状态,使用--skip-extended-insert取消选项。
--fields-terminated-by                导出文件中忽略给定字段。与--tab选项一起使用,不能用于--databases和--all-databases选项
--fields-enclosed-by                输出文件中的各个字段用给定字符包裹。与--tab选项一起使用,不能用于--databases和--all-databases选项
--fields-optionally-enclosed-by                输出文件中的各个字段用给定字符选择性包裹。与--tab选项一起使用,不能用于--databases和--all-databases选项
--fields-escaped-by                输出文件中的各个字段忽略给定字符。与--tab选项一起使用,不能用于--databases和--all-databases选项
--flush-logs,  -F                开始导出之前刷新日志。请注意:假如一次导出多个数据库(使用选项--databases或者--all-databases),将会逐个数据库刷新日志。除使用--lock-all-tables或者--master-data外。在这种情况下,日志将会被刷新一次,相应的所以表同时被锁定。因此,如果打算同时导出和刷新日志应该使用--lock-all-tables 或者--master-data 和--flush-logs。
--flush-privileges                在导出mysql数据库之后,发出一条FLUSH  PRIVILEGES 语句。为了正确恢复,该选项应该用于导出mysql数据库和依赖mysql数据库数据的任何时候。
--force,  -f                在导出过程中忽略出现的SQL错误。
--help,  -?                显示帮助信息并退出。
--hex-blob                使用十六进制格式导出二进制字符串字段。如果有二进制数据就必须使用该选项。影响到的字段类型有BINARY、VARBINARY、BLOB。
--host, -h                需要导出的主机信息
--ignore-table                不导出指定表。指定忽略多个表时,需要重复多次,每次一个表。每个表必须同时指定数据库和表名。
--insert-ignore                在插入行时使用INSERT IGNORE语句.
--lines-terminated-by                输出文件的每行用给定字符串划分。与--tab选项一起使用,不能用于--databases和--all-databases选项。
--lock-all-tables,  -x                提交请求锁定所有数据库中的所有表,以保证数据的一致性。这是一个全局读锁,并且自动关闭--single-transaction 和--lock-tables 选项。
--lock-tables,  -l                开始导出前,锁定所有表。用READ  LOCAL锁定表以允许MyISAM表并行插入。对于支持事务的表例如InnoDB和BDB,--single-transaction是一个更好的选择,因为它根本不需要锁定表。请注意当导出多个数据库时,--lock-tables分别为每个数据库锁定表。因此,该选项不能保证导出文件中的表在数据库之间的逻辑一致性。不同数据库表的导出状态可以完全不同。
--log-error                附加警告和错误信息到给定文件
--max_allowed_packet                服务器发送和接受的最大包长度。
--net_buffer_length                TCP/IP和socket连接的缓存大小。
--no-autocommit                使用autocommit/commit 语句包裹表。
--no-create-db,  -n                只导出数据,而不添加CREATE DATABASE 语句。
--no-create-info,  -t                只导出数据,而不添加CREATE TABLE 语句。
--no-data, -d                不导出任何数据,只导出数据库表结构。
--no-set-names,  -N                等同于--skip-set-charset
--opt                等同于--add-drop-table,  --add-locks, --create-options, --quick, --extended-insert, --lock-tables,  --set-charset, --disable-keys 该选项默认开启,  可以用--skip-opt禁用.
--order-by-primary                如果存在主键,或者第一个唯一键,对每个表的记录进行排序。在导出MyISAM表到InnoDB表时有效,但会使得导出工作花费很长时间。 
--password, -p                连接数据库密码
--pipe(windows系统可用)                使用命名管道连接mysql
--port, -P                连接数据库端口号
--protocol                使用的连接协议,包括:tcp, socket, pipe, memory.
--quick, -q                不缓冲查询,直接导出到标准输出。默认为打开状态,使用--skip-quick取消该选项。
--quote-names,-Q                使用(`)引起表和列名。默认为打开状态,使用--skip-quote-names取消该选项。
--replace                使用REPLACE INTO 取代INSERT INTO.
--result-file,  -r                直接输出到指定文件中。该选项应该用在使用回车换行对(\\r\\n)换行的系统上(例如:DOS,Windows)。该选项确保只有一行被使用。
--routines, -R                导出存储过程以及自定义函数。
--set-charset                添加'SET NAMES  default_character_set'到输出文件。默认为打开状态,使用--skip-set-charset关闭选项。
--single-transaction                该选项在导出数据之前提交一个BEGIN SQL语句,BEGIN 不会阻塞任何应用程序且能保证导出时数据库的一致性状态。它只适用于多版本存储引擎,仅InnoDB。本选项和--lock-tables 选项是互斥的,因为LOCK  TABLES 会使任何挂起的事务隐含提交。要想导出大表的话,应结合使用--quick 选项。
--dump-date                将导出时间添加到输出文件中。默认为打开状态,使用--skip-dump-date关闭选项。
--skip-opt                禁用–opt选项.
--socket,-S                指定连接mysql的socket文件位置,默认路径/tmp/mysql.sock
--tab,-T                为每个表在给定路径创建tab分割的文本文件。注意:仅仅用于mysqldump和mysqld服务器运行在相同机器上。注意使用--tab不能指定--databases参数
--tables                覆盖--databases (-B)参数,指定需要导出的表名,在后面的版本会使用table取代tables。
--triggers                导出触发器。该选项默认启用,用--skip-triggers禁用它。
--tz-utc                在导出顶部设置时区TIME_ZONE='+00:00' ,以保证在不同时区导出的TIMESTAMP 数据或者数据被移动其他时区时的正确性。
--user, -u                指定连接的用户名。
--verbose, --v                输出多种平台信息。
--version, -V                输出mysqldump版本信息并退出
--where, -w                只转储给定的WHERE条件选择的记录。请注意如果条件包含命令解释符专用空格或字符,一定要将条件引用起来。
--xml, -X                导出XML格式.

一、MySQL 5.7

1.添加存储库

在Debian 11和Debian 10上下载并安装 mysql 存储库设置包。

# wget https://dev.mysql.com/get/mysql-apt-config_0.8.18-1_all.deb
# dpkg -i mysql-apt-config_0.8.18-1_all.deb
Debian buster->MySQL Server & Cluster->mysql-5.7->Ok

2.安装MySQL

更新存储库索引

# apt update
Hit:1 http://deb.debian.org/debian bullseye InRelease
Hit:2 http://deb.debian.org/debian-security bullseye-security InRelease
Hit:3 http://deb.debian.org/debian bullseye-updates InRelease
Get:4 http://repo.mysql.com/apt/debian buster InRelease [22.1 kB]
Hit:5 http://nginx.org/packages/debian bullseye InRelease
Err:4 http://repo.mysql.com/apt/debian buster InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 467B942D3A79BD29
Reading package lists... Done
W: GPG error: http://repo.mysql.com/apt/debian buster InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 467B942D3A79BD29
E: The repository 'http://repo.mysql.com/apt/debian buster InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

在Debian 11上导入缺失的GPG密钥

# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
Executing: /tmp/apt-key-gpghome.3evQg0TN8t/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
gpg: key 467B942D3A79BD29: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1
# apt update
Hit:1 http://deb.debian.org/debian bullseye InRelease
Hit:2 http://deb.debian.org/debian-security bullseye-security InRelease
Hit:3 http://deb.debian.org/debian bullseye-updates InRelease
Get:4 http://repo.mysql.com/apt/debian buster InRelease [22.1 kB]
Hit:5 http://nginx.org/packages/debian bullseye InRelease
Get:6 http://repo.mysql.com/apt/debian buster/mysql-5.7 Sources [909 B]
Get:7 http://repo.mysql.com/apt/debian buster/mysql-apt-config amd64 Packages [565 B]
Get:8 http://repo.mysql.com/apt/debian buster/mysql-5.7 amd64 Packages [5,687 B]
Get:9 http://repo.mysql.com/apt/debian buster/mysql-tools amd64 Packages [8,237 B]
Fetched 37.5 kB in 3s (11.5 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
1 package can be upgraded. Run 'apt list --upgradable' to see it.

安装MySQL 5.7服务包

# apt install -y mysql-community-server
...
mysql-community-server (5.7.42-1debian10) ...
Setting up libmecab2:amd64 (0.996-14+b4) ...
Setting up mysql-common (5.8+1.0.7) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Setting up psmisc (23.4-2) ...
Setting up perl-modules-5.32 (5.32.1-4+deb11u2) ...
Setting up libatomic1:amd64 (10.2.1-6) ...
Setting up libnuma1:amd64 (2.0.12-1+b1) ...
Setting up libaio1:amd64 (0.3.112-9) ...
Setting up libgdbm6:amd64 (1.19-2) ...
Setting up mysql-community-client (5.7.42-1debian10) ...
Setting up mysql-client (5.7.42-1debian10) ...
Setting up libgdbm-compat4:amd64 (1.19-2) ...
Setting up libperl5.32:amd64 (5.32.1-4+deb11u2) ...
Setting up perl (5.32.1-4+deb11u2) ...
Setting up mysql-community-server (5.7.42-1debian10) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
Processing triggers for libc-bin (2.31-13+deb11u6) ...

3.启动MySQL服务

设置MySQL服务为自启动。

# systemctl restart mysql
# systemctl enable mysql
Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mysql
# systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: active (running) since Sun 2023-07-09 01:02:54 EDT; 16s ago
   Main PID: 28128 (mysqld)
      Tasks: 27 (limit: 1150)
     Memory: 172.5M
        CPU: 483ms
     CGroup: /system.slice/mysql.service
             └─28128 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/my>
             
Jul 09 01:02:54 proud-delight-1.localdomain systemd[1]: Starting MySQL Communit>
Jul 09 01:02:54 proud-delight-1.localdomain systemd[1]: Started MySQL Community>

4.MySQL安全配置

# mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root:

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: n
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y

 - Dropping test database...
   Success.

 - Removing privileges on test database...
   Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

5.客户端连接

# mysql -u root -p

6.允许远程连接

# open MySQL port 3306 on the firewall
sudo ufw allow mysql

# allow specific addresses to connect to mysql
sudo ufw allow from 192.168.100.222 to any port 3306

二、安装PHP

1.更新系统

# apt update
# apt -y upgrade
# apt reboot

2.添加存储库

安装依赖

# apt install -y lsb-release ca-certificates apt-transport-https software-properties-common gnupg2
...
Creating config file /etc/apt/apt.conf.d/50unattended-upgrades with new version
Created symlink /etc/systemd/system/multi-user.target.wants/unattended-upgrades.service → /lib/systemd/system/unattended-upgrades.service.
Synchronizing state of unattended-upgrades.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable unattended-upgrades
Setting up python3-software-properties (0.96.20.2-2.1) ...
Setting up gir1.2-glib-2.0:amd64 (1.66.1-1+b1) ...
Setting up libpolkit-agent-1-0:amd64 (0.105-31+deb11u1) ...
Setting up policykit-1 (0.105-31+deb11u1) ...
Setting up gir1.2-packagekitglib-1.0 (1.2.2-2) ...
Setting up python3-gi (3.38.0-2) ...
Setting up packagekit (1.2.2-2) ...
Created symlink /etc/systemd/user/sockets.target.wants/pk-debconf-helper.socket → /usr/lib/systemd/user/pk-debconf-helper.socket.
Setting up packagekit-tools (1.2.2-2) ...
Setting up software-properties-common (0.96.20.2-2.1) ...
Processing triggers for libc-bin (2.31-13+deb11u6) ...
Processing triggers for dbus (1.12.24-0+deb11u1) ...

添加PHP包APT存储库到服务器

# echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
deb https://packages.sury.org/php/ bullseye main

导入存储库密钥

# wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK

执行包索引更新以确认存储库已添加

# apt update
Hit:1 http://deb.debian.org/debian bullseye InRelease
Hit:2 http://deb.debian.org/debian-security bullseye-security InRelease
Hit:3 http://deb.debian.org/debian bullseye-updates InRelease
Hit:4 http://repo.mysql.com/apt/debian buster InRelease
Hit:5 https://packages.sury.org/php bullseye InRelease
Hit:6 http://nginx.org/packages/debian bullseye InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
8 packages can be upgraded. Run 'apt list --upgradable' to see them.

3.安装PHP

# apt install php8.0
...
After this operation, 29.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...
Enabling module status.
Enabling module reqtimeout.
Enabling conf charset.
Enabling conf localized-error-pages.
Enabling conf other-vhosts-access-log.
Enabling conf security.
Enabling conf serve-cgi-bin.
Enabling site 000-default.
info: Switch to mpm prefork for package libapache2-mod-php8.0
Module mpm_event disabled.
Enabling module mpm_prefork.
info: Executing deferred 'a2enmod php8.0' for package libapache2-mod-php8.0
Enabling module php8.0.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xe" for details.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Setting up php8.0 (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff) ...
Processing triggers for libc-bin (2.31-13+deb11u6) ...
Processing triggers for php8.0-cli (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff) ...
Processing triggers for libapache2-mod-php8.0 (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff) ...
# php -v
PHP 8.0.29 (cli) (built: Jun  9 2023 07:37:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.29, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.29, Copyright (c), by Zend Technologies

4.安装PHP扩展

# apt install php8.0-{mysql,cli,common,imap,ldap,xml,fpm,curl,mbstring,zip}
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
php8.0-cli is already the newest version (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff).
php8.0-cli set to manually installed.
php8.0-common is already the newest version (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff).
php8.0-common set to manually installed.
The following additional packages will be installed:
  libc-client2007e libxslt1.1 libzip4 mlock
Suggested packages:
  uw-mailutils php-pear
The following NEW packages will be installed:
  libc-client2007e libxslt1.1 libzip4 mlock php8.0-curl php8.0-fpm php8.0-imap php8.0-ldap php8.0-mbstring php8.0-mysql
  php8.0-xml php8.0-zip
0 upgraded, 12 newly installed, 0 to remove and 7 not upgraded.
Need to get 3,154 kB of archives.

Creating config file /etc/php/8.0/mods-available/simplexml.ini with new version

Creating config file /etc/php/8.0/mods-available/xml.ini with new version

Creating config file /etc/php/8.0/mods-available/xmlreader.ini with new version

Creating config file /etc/php/8.0/mods-available/xmlwriter.ini with new version

Creating config file /etc/php/8.0/mods-available/xsl.ini with new version
Setting up php8.0-imap (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff) ...

Creating config file /etc/php/8.0/mods-available/imap.ini with new version
Processing triggers for libapache2-mod-php8.0 (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff) ...
Processing triggers for libc-bin (2.31-13+deb11u6) ...
Processing triggers for php8.0-cli (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff) ...
Processing triggers for php8.0-fpm (1:8.0.29-1+0~20230609.54+debian11~1.gbp7b07ff) ...
NOTICE: Not enabling PHP 8.0 FPM by default.
NOTICE: To enable PHP 8.0 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php8.0-fpm
NOTICE: You are seeing this message because you have apache2 package installed.

检查加载的PHP模块

# php -m

5.配置Nginx

Nginx 新版本没有 sites-enabledsites-available ;它还有一个不同的 PHP CGI 设置。

NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
/etc/php/8.0/fpm/php.ini

最终配置版本为:

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
您是第 68076 位访客