[email protected]’s Humble Abode       博  客   时 间 线   归  档   关 于 与 友 链



MySQL installation by CLI
Published on Wed May 25 2022 23:30:00 GMT+0000

命令行安装 MySQL

配置环境变量

新建变量

1
2
3
变量名:MYSQL_HOME

值:安装路径

添加PATH路径

1
%MYSQL_HOME%\bin

安装MySQL

初始化并记录初始化密码

1
mysqld --initialize --console

将MySQL安装为Windows的服务

1
mysqld -install

启动MySQL

1
net start MySQL

登陆数据库,并输入前面记录的临时密码

1
mysql -u root -p

修改自己的密码:123456 ,命令一定要包括分号

1
2
alter user 'root'@'localhost' identified by '123456';
alter user 'root'@'localhost' identified with mysql_native_password by '123456';

最后提交

1
commit;

补充:删除MySQL

1
2
net stop mysql
mysqld -remove

补充:连接MySQL

SpringBoot

1
2
3
4
5
#MYSQL链接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306[(可选)/database]?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

NestJS

引入包

1
yarn add @nestjs/typeorm typeorm mysql2

导入到app.module.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
entities: [],
synchronize: true,
}),
],
})
export class AppModule {}

另外,我们可以创建 ormconfig.json ,而不是将配置对象传递给 forRoot()

1
2
3
4
5
6
7
8
9
10
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "root",
"database": "test",
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": true
}

然后,我们可以不带任何选项地调用 forRoot() :

app.module.ts

1
2
3
4
5
6
7
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [TypeOrmModule.forRoot()],
})
export class AppModule {}