注意(一):

host:域名  /  localhost
username:用户名
password:密码
port:端口,默认3306
DatabaseName:数据库名称

注意(二):

PHP5.6无法连接MySQL解决

[php.ini]配置修改

/etc/php/5.6/cli/php.ini

/etc/php/5.6/fpm/php.ini

删除分号

;always_populate_raw_post_data

MySQL的[mysqld]配置修改

sudo nano /etc/mysql/mysql.conf.d/

[mysqld]配置修改

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
default_authentication_plugin=mysql_native_password

MySQL 8.0开始安装

安装 MySQL 8.0 所需的软件包

sudo apt install dirmngr software-properties-common apt-transport-https curl lsb-release ca-certificates -y

导入 MySQL 社区存储库

导入 GPG 密钥

注意:显示unsigned,请修改RPM-GPG-KEY-mysql-xxxx

curl -fsSL https://repo.mysql.com/RPM-GPG-KEY-mysql-2023 | gpg --dearmor | sudo tee /usr/share/keyrings/mysql.gpg > /dev/null

添加 MySQL 8.0 APT 存储库

echo "deb [signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/debian $(lsb_release -sc) mysql-8.0" | sudo tee /etc/apt/sources.list.d/mysql.list

导入开发 MySQL 8.0 工具

echo "deb [signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/debian $(lsb_release -sc) mysql-tools" | sudo tee -a /etc/apt/sources.list.d/mysql.list
echo "deb-src [signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/debian $(lsb_release -sc) mysql-tools" | sudo tee -a /etc/apt/sources.list.d/mysql.list

刷新系统的软件包列表

sudo apt update

通过 APT 命令安装 MySQL 8.0

sudo apt install mysql-community-server

设置 MySQL 8.0 数据库根密码:图形化界面 TAB + Enter

MySQL 5.7开始安装

注意:MySQL 5.7 存储库尚不可用于 Debian 11 (Bullseye)

下载并安装 mysql 存储库设置包

下载

wget https://repo.mysql.com/mysql-apt-config_0.8.18-1_all.deb

安装

安装依赖软件

sudo apt install gnupg

安装命令

sudo dpkg -i mysql-apt-config_0.8.18-1_all.deb

图形界面设置

debain buster => MySQL Server & Cluster(Currently selected:mysql-8.0) => mysql-5.7 => ok

导入GPG 密钥

注:... the public key is not available: NO_PUBKEY xxxxxx

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys xxxxxx
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B7B3B788A8D3785C

安装 MySQL 5.7 服务器包

sudo apt update
sudo apt install -y mysql-community-server

MySQL操作

常规操作

mysql -V
sudo service mysql start
sudo service mysql restart
sudo service mysql stop

登录mysql

mysql -u用户名 -p密码

连接远程数据库

mysql -h <host> -P <port> -u<username> -p<password>

用户管理

创建

默认创建

create user 'username' @'host' identified by 'password';

适配PHP5.x

CREATE USER 'username'@'host' IDENTIFIED WITH mysql_native_password BY 'password';

查看

select user,host from mysql.user;

删除

drop user 'username'@'host';

数据库操作

创建

create database DatabaseName;

删除

drop database DatabaseName;

查看

show databases;
use DatabaseName;
show tables;
SELECT * FROM tableName;

导出SQL文件

mysqldump -u 'root' -h 'localhost' -p DatabaseName > /../Other_DatabaseName.sql

导入SQL文件

mysql -u 'root' -h 'localhost' -p DatabaseName < /../Other_DatabaseName.sql

权限管理

开放权限

GRANT all ON DatabaseName.* TO 'uername'@'host';

立即生效

flush privileges;