I am trying to connect mysql with native react. I am trying to view the contents from the database but keep getting a timeout error. I am convinced that my mysql credentials are correct.
Is there something I am missing ?
I followed this tutorial https://dev.to/saulojoab/how-to-get-data-from-an-mysql-database-in-react-native-53a4
package.json
{
"name": "check",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
},
"dependencies": {
"@react-navigation/drawer": "^6.5.0",
"@react-navigation/native": "^6.0.13",
"@react-navigation/stack": "^6.3.2",
"body-parser": "^1.20.2",
"expo": "~46.0.16",
"expo-status-bar": "~1.4.0",
"express": "^4.18.2",
"mysql": "^2.18.1",
"react": "18.0.0",
"react-native": "0.69.6",
"react-native-gesture-handler": "2.5.0",
"react-native-reanimated": "~2.9.1",
"react-native-safe-area-context": "4.3.1",
"react-native-snap-carousel": "https://github.com/ram33z/react-native-snap-carousel"
},
"devDependencies": {
"@babel/core": "^7.12.9"
},
"private": true
}
route.js file
const express = require('express');
const mysql = require('mysql');
const app = express();
const connection = mysql.createPool({
host: 'localhost',
user: 'check',
database: 'mydata',
});
// Test the MySQL connection
connection.getConnection(function (err, connection) {
if (err) {
console.error('Error connecting to MySQL:', err);
} else {
console.log('Connected to MySQL database');
connection.release();
}
});
// Creating a GET route that returns data from the 'test' table.
app.get('/test', function (req, res) {
// Executing the MySQL query (select all data from the 'test' table).
connection.query('SELECT * FROM mydata.test', function (error, results, fields) {
if (error) {
console.error('Error executing MySQL query:', error);
res.status(500).send('Internal Server Error');
} else {
res.send(results);
}
});
});
// Starting our server.
app.listen(3306, () => {
console.log('Go to http://localhost:3306/test to see the data.');
});
error:
Error connecting to MySQL: Error: Handshake inactivity timeout
at Handshake.<anonymous> (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/Protocol.js:160:17)
at Handshake.emit (node:events:513:28)
at Handshake._onTimeout (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:124:8)
at Timer._onTimeout (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/Timer.js:32:23)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)
--------------------
at Protocol._enqueue (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/Users/raheem/Documents/app/node_modules/mysql/lib/Connection.js:116:18)
at Pool.getConnection (/Users/raheem/Documents/app/node_modules/mysql/lib/Pool.js:48:16)
at Object.<anonymous> (/Users/raheem/Documents/route.js:13:12)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
code: 'PROTOCOL_SEQUENCE_TIMEOUT',
fatal: true,
timeout: 10000
}
Error executing MySQL query: Error: Handshake inactivity timeout
at Handshake.<anonymous> (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/Protocol.js:160:17)
at Handshake.emit (node:events:513:28)
at Handshake._onTimeout (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:124:8)
at Timer._onTimeout (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/Timer.js:32:23)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)
--------------------
at Protocol._enqueue (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/raheem/Documents/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/Users/raheem/Documents/app/node_modules/mysql/lib/Connection.js:116:18)
at Pool.getConnection (/Users/rasheed/Documents/app/team_08-main/node_modules/mysql/lib/Pool.js:48:16)
at Pool.query (/Users/raheem/Documents/app/node_modules/mysql/lib/Pool.js:202:8)
at /Users/raheem/Documents/app/route.js:25:16
at Layer.handle [as handle_request] (/Users/raheem/Documents/app/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/raheem/Documents/app/node_modules/express/lib/router/route.js:144:13)
at Route.dispatch (/Users/raheem/Documents/app/node_modules/express/lib/router/route.js:114:3)
at Layer.handle [as handle_request] (/Users/raheem/Documents/app/node_modules/express/lib/router/layer.js:95:5) {
code: 'PROTOCOL_SEQUENCE_TIMEOUT',
fatal: true,
timeout: 10000
}