XuSenfeng

个人站

复读了,更新随缘,有的文件不全或者图片缺失具体看我的笔记库(https://github.com/XuSenfeng/note)


数据库安装

目录

安装

linux

(base) jiao@ubuntu:~$ sudo apt-get install mysql-server
(base) jiao@ubuntu:~$ sudo apt-get install mysql-client

server安装的是服务器, 用于生成不同的保存数据以及提供查询

client操作的工具

(base) jiao@ubuntu:~$ systemctl status mysql.service 

查看服务是否运行

设置初始化(密码设置等)

(base) jiao@ubuntu:~$ sudo mysql_secure_installation 
[sudo] jiao 的密码: 

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

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


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

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

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


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

 ... skipping.
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! 
(base) jiao@ubuntu:~$ systemctl status mysql.service 
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since Fri 2023-01-20 15:18:46 CST; 1h 26min ago
 Main PID: 1215 (mysqld)
    Tasks: 29 (limit: 2284)
   CGroup: /system.slice/mysql.service
           └─1215 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Jan 20 15:18:13 ubuntu systemd[1]: Starting MySQL Community Server...
Jan 20 15:18:46 ubuntu systemd[1]: Started MySQL Community Server.


实际的使用

(base) jiao@ubuntu:~$ sudo mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.40-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2022, 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> show schemas
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.30 sec)

mysql> exit
Bye

windows

MySQL :: Download MySQL Community Server (Archived Versions)

创建配置文件my.ini, 建议放在软件存放的目录下面

[mysqld]
# 设置端口
port=3306
# 安装目录
basedir=E:\\alearn\\mysql\\mysql-5.7.31-winx64
# 创建的数据
datadir=E:\\alearn\\mysql\\mysql-5.7.31-winx64\\data

命令行查找配置文件, 可以安装多个版本

C:\Users\JHY>"E:\alearn\mysql\mysql-5.7.31-winx64\bin\mysqld.exe" --help --verbose
mysqld: Could not create or access the registry key needed for the MySQL application
to log to the Windows EventLog. Run the application with sufficient
privileges once to create the key, add the key manually, or turn off
logging for that application.
mysqld: Can't change dir to 'E:\alearn\mysql\mysql-5.7.31-winx64\data\' (Errcode: 2 - No such file or 

...

Default options are read from the following files in the given order:
C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf E:\alearn\mysql\mysql-5.7.31-winx64\my.ini E:\alearn\mysql\mysql-5.7.31-winx64\my.cnf

查找文件的优先级

  • 初始化(管理员权限)
C:\Users\JHY>"E:\alearn\mysql\mysql-5.7.31-winx64\bin\mysqld.exe" --initialize-insecure

初始化账户, 账户没有密码

  • 启动
  1. C:\Users\JHY>"E:\alearn\mysql\mysql-5.7.31-winx64\bin\mysqld.exe" --initialize-insecure
    

    缺点, 需要开启终端, 比较麻烦

  2. 制作成一个服务
C:\Windows\system32>"E:\alearn\mysql\mysql-5.7.31-winx64\bin\mysqld.exe" --install mysql57
Service successfully installed.

C:\Windows\system32>net start mysql57
mysql57 服务正在启动 .
mysql57 服务已经启动成功。


C:\Windows\system32>net stop mysql57
mysql57 服务正在停止.
mysql57 服务已成功停止。

C:\Windows\system32>"E:\alearn\mysql\mysql-5.7.31-winx64\bin\mysqld.exe" --remove mysql57
删除
  • 实际使用
C:\Windows\system32>"E:\alearn\mysql\mysql-5.7.31-winx64\bin\mysql.exe" -h 127.0.0.1 -P 3306 -u root -p

-h使用的服务器, 本机可以忽略

-P端口号, 同上

-u用户

-p密码

C:\Windows\system32>net start mysql57
mysql57 服务正在启动 .
mysql57 服务已经启动成功。


C:\Windows\system32>"E:\alearn\mysql\mysql-5.7.31-winx64\bin\mysql.exe" -h 127.0.0.1 -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye

配置文件

目前大多数使用默认的

密码

修改设置密码

mysql> set password = password("jhy030116");
Query OK, 0 rows affected, 1 warning (0.00 sec)

忘记密码

在配置文件中添加, 会进行跳过登录环节

[mysqld]

skip-grant-tables=1

重启服务

net stop mysql57
net start mysql57

重启之后就不需要密码进入

mysql -u root -p

进入之后

use mysql;
update user set authentication_string = password("新密码"),password_last_change=now() where user='root';

退出之后把之前的配置文件中的信息删除

之后重启就可以使用新的密码进行登录