用Nodejs连接 mysql

安装mysql 和nodejs

我是用docker安装的mysql

$ docker pull mariadb  #下载mariadb镜像
$ sudo pacman -S npm #安装npm
$ docker run --name mariadb -v ~/Documents/msql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=qinjingfei -d mariadb #运行mariadb 

mariadb 是mysql 的社区版本, 可以用docker ps 来看mariadb是否在运行

建立连接

首先安装mysql module

$ npm install mysql

然后,再新建一个demo.js文件

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost", // 需要用`docker inspect mariadb` 来查看host
  user: "yourusername", // root
  password: "yourpassword"  //qinjingfei
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

因为,我是用docker 运行的mariadb。所以,我需要用docker inspect mariadb来查看host. 下面是部分截图

可以看到 ipaddress是172.17.0.3, 所以host 就是172.17.0.3.

然后,就可以运行这个文件了

$ node demo.js

如果,一切都没有错误的话会得到下面的结果

Connected!

至此,我们就用Nodejs成功的连接上mysql了

安装mysql

mysql

安装

docker pull mysql:latest
latest: Pulling from library/mysql
Digest: sha256:96edf37370df96d2a4ee1715cc5c7820a0ec6286551a927981ed50f0273d9b43
Status: Image is up to date for mysql:latest

现在运行mysql服务器

$ docker run  -d --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" mysql

docker inspect test-mysql 来查看ip地址和端口

现在用mysql客户端连接服务器

$ mysql -uroot -pqinjingfei -h 172.17.0.2 -P 3306
  • -u

user

  • -p

password

  • -P

port

  • -h

host

基本命令

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)


mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.01 sec)

mysql> select * from db;
...

PHP

PHP 是什么

PHP(“Hypertext Preprocessor”, 超文本预处理器)是一种被广泛应用的开放源代码的多用途脚本语言。

PHP能做什么

  • 收集表单数据
  • 生成动态网页
  • 发送/接受cookies

安装

sudo apt install php php-mysql

用PHP 连接 mysql

demo.php

<?php
        $dbhost = '172.17.0.2:3306';
        $dbuser = 'root';
        $dbpass = 'mypassword';
        $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
        if(! $conn ) {
           die('Could not connect: ' . mysql_error());
        }
        echo 'Connected successfully';
        mysqli_close($conn);
?>

执行

$ php demo.php
Connected successfully⏎