内容更新于: 2022-06-19 02:55:57
数据库连接
前言:本文主要记录了mysql的链接方式,并以nodejs和java等语言为例。对链接用到的一些方法进行记录,同时也可补充一些原理方面的内容。
使用mysql二进制方式连接
您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库。
实例
以下是从命令行中连接mysql服务器的简单实例:
-bash-4.2# ./mysql -u root -p
Enter password: xxxx
在登录成功后会出现 mysql> 命令提示窗口,你可以在上面执行任何 SQL 语句。
以上命令执行后,登录成功输出结果如下:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
在以上实例中,我们使用了root用户登录到mysql服务器,当然你也可以使用其他mysql用户登录。
如果用户权限足够,任何用户都可以在mysql的命令提示窗口中进行SQL操作。
退出 mysql> 命令提示窗口可以使用 exit 命令,如下所示:
mysql> exit
Bye
eggjs中链接数据库
在eggjs中去链接mysql的话是很方便的,只要在配置文件中配置好数据库的相关信息就可以了直接使用了。
1.egg-mysql
// config/config.default.js
module.exports = (appInfo) => {
const config = {
// xxx
};
// 数据库配置
config.mysql = {
// database configuration
client: {
// host
host: '116.62.145.208',
// port
port: '3306',
// username
user: 'root',
// password
password: 'password',
// database
database: 'blog',
},
// load into app, default is open
app: true,
// load into agent, default is close
agent: false,
}
return {
...config
}
}
2.egg-sequelize
// condig/config.default.js
// 即数据库的一些配置
config.sequelize = {
dialect: 'mysql',
host: '116.62.145.208',
port: 3306,
database: 'blog',
username: 'root',
password: 'kakdkem9910##$$kdkdk',
timezone: '+08:00',
app: true,
}
本文结束
