简体   繁体   中英

Cloud SQl, MySQL database Tables not found when created from NodeJs

I have created a Google cloud mySql instance that I would like to use to store some greenhouse information. I have set up the instance and I have also connected my NodeJs program to the instance. I also have the mysql cloud proxy running with the following command

.\cloud_sql_proxy.exe -instances=my-instance-name=tcp:3306

The following code was used to connect to the database.

var dbConnected = false;
const config = ConnectionOptions = {
  host: '127.0.0.1',
  user: 'Username',
  password: 'Password',
  database: 'GreehouseInfo', 
  dialect: 'mysql',
  synchronize: true,
}


const connection = mysql.createConnection(config);

async function ConnectToDatabse() {
    await connection.connect((err) => {
      if (err) throw err;
      console.log('Connected!');
      dbConnected = true;
    });
}

Now I have also used the following query to Create some of my Tables dynamically and the code looks something like this #

async function CreateTables() {
  return new Promise(resolve => {
    if (dbConnected){
        
        var sql = "CREATE TABLE IF NOT EXISTS Users(User_ID INT AUTO_INCREMENT PRIMARY KEY,UserName VARCHAR(255) NOT NULL,Password VARCHAR(255))";
        connection.query(sql, function (err, result) {
            console.log(result)
        if (err) console.log(err);
        
      });
    }
    console.log("Table created");
    resolve();
  });
}

The query does not throw any errors when I start the node service and says that the tables were created and when I connect to the DB Instance using the command line and the following command mysql -u RichardTeunen -p --host 127.0.0.1 --port 3306 then I can use the following command show tables; To see my tables and then I can see the table users that I have just created

我从 mysql CLI 中退出

The issue i am experiencing is when I try to perform any queries on the table. I have created the following function for performing queries for me.

async function PerformQuery(querytext) {
  return new Promise(resolve => {
    if (dbConnected){
        var sql = querytext;
        connection.query(sql, function (err, result) {
        if (err) console.log(err.message);
        
      });
    }
    console.log("Query performed");
    resolve();
  });
}

I have tried Select and Insert statements with this query and I always get the error that

The table GreehouseInfo.USERS does not exist

However as I just showed the Table does exist, so I am unsure why this is occurring. I have read that this happens if you backup your device to another location and then you should take some files with you but I am unsure what database files I may have to copy to my local device to make the connection valid on this device. Any advice would be much appreciated.

As far as I can see, your table is User (see your create script) not USERS (See your SELECT ERROR). Case sensitive matters on Linux env ;)

See docs

See examplehere .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM