阅读量:4
在CentOS上安装Node.js和MySQL的步骤如下:
安装Node.js
-
更新系统包
sudo yum update -y -
安装NodeSource Node.js 14.x仓库
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash - -
安装Node.js和npm
sudo yum install -y nodejs -
验证安装
node -v npm -v
安装MySQL
-
安装MySQL服务器
sudo yum install -y mysql-server -
启动MySQL服务
sudo systemctl start mysqld -
设置MySQL开机自启动
sudo systemctl enable mysqld -
获取临时密码 安装完成后,MySQL会生成一个临时密码,你可以在日志文件中找到它:
sudo grep 'temporary password' /var/log/mysqld.log -
运行安全脚本 使用临时密码登录MySQL并运行安全脚本,设置新密码并删除匿名用户、禁止远程root登录等:
sudo mysql_secure_installation -
登录MySQL 使用新密码登录MySQL:
mysql -u root -p
配置Node.js连接MySQL
-
创建一个新的Node.js项目(如果还没有)
mkdir myproject cd myproject npm init -y -
安装MySQL客户端库
npm install mysql -
编写Node.js代码连接MySQL 创建一个名为
index.js的文件,并添加以下代码:const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'your_new_password', // 替换为你设置的新密码 database: 'your_database_name' // 替换为你想要连接的数据库名称 }); connection.connect((err) => { if (err) throw err; console.log('Connected to the MySQL server.'); }); connection.query('SELECT * FROM your_table_name', (err, results, fields) => { if (err) throw err; console.log(results); }); connection.end(); -
运行Node.js脚本
node index.js
通过以上步骤,你就可以在CentOS上成功安装Node.js和MySQL,并配置Node.js连接MySQL数据库。
以上就是关于“CentOS Node.js如何安装MySQL”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm