Preamble
Hello friends, I have come back, continuing with the series about Nodejs cơ bản
, in this article I will introduce to everyone how to install and connect mysql to nodejs.
Proceed
Install mysql
To connect to mysql, we must have mysql correct. You can install workbench
Or install mysql directly in the terminal like me: v. Because this article mainly talks about connecting mysql in Nodejs
, I would like to show you how to install mysql
on ubuntu
only: v
- First, you need to update the apt package with the command
1 2 | sudo apt update |
- next is installing
Mysql
1 2 | sudo apt install mysql <span class="token operator">-</span> server mysql <span class="token operator">-</span> client <span class="token operator">-</span> y |
- Next is running the command
1 2 | sudo mysql_secure_installation |
To increase the security of mysql
more, I am not sure if you know this, please comment below for me to add. You guys just press y
until it is All done!
is to be
- Next login with root you run the command
1 2 | sudo mysql |
then run to the next command
1 2 3 | <span class="token constant">ALTER</span> <span class="token constant">USER</span> <span class="token single-quoted-string string">'root'</span> @ <span class="token single-quoted-string string">'localhost'</span> <span class="token constant">IDENTIFIED</span> <span class="token constant">WITH</span> mysql_native_password <span class="token constant">BY</span> <span class="token single-quoted-string string">'your_password'</span> <span class="token punctuation">;</span> <span class="token constant">FLUSH</span> <span class="token constant">PRIVILEGES</span> <span class="token punctuation">;</span> |
Where your_password
is the pass you want to set.
OK now try running the command
1 2 | mysql <span class="token operator">-</span> u root <span class="token operator">-</span> p |
Then enter the password just changed at the top to see, run the next command
1 2 | show databases <span class="token punctuation">;</span> |
This is ok. Next we will create a database to connect to Nodejs
. Simply run this command
1 2 | craete database dbTest <span class="token punctuation">;</span> |
is created a new db: v. Note that dbTest
here is the name of the database. You can refer to the command manipulation with mysql here .
Connect mysql to Nodejs
To connect to mysql, we need to install the MYSQL
module as we did in the previous posts to install a module we just need to run the command.
1 2 | yarn add mysql <span class="token operator">--</span> save |
Next in the index.js file we will require
mysql
module. Make the declaration on the connection as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <span class="token keyword">var</span> conn <span class="token operator">=</span> mysql <span class="token punctuation">.</span> <span class="token function">createConnection</span> <span class="token punctuation">(</span> <span class="token punctuation">{</span> host <span class="token punctuation">:</span> <span class="token single-quoted-string string">'localhost'</span> <span class="token punctuation">,</span> user <span class="token punctuation">:</span> <span class="token single-quoted-string string">'root'</span> <span class="token punctuation">,</span> password <span class="token punctuation">:</span> <span class="token single-quoted-string string">''</span> <span class="token punctuation">,</span> database <span class="token punctuation">:</span> <span class="token single-quoted-string string">'dbTest'</span> <span class="token punctuation">,</span> charset <span class="token punctuation">:</span> <span class="token single-quoted-string string">'utf8_general_ci'</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">connect</span> <span class="token punctuation">(</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">throw</span> err <span class="token punctuation">.</span> stack <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token double-quoted-string string">"connect success"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> |
The first is to declare host, user, password (if any), database name, type unicode. Next is the connect function here, I make the connection. If there is an error, print the error or else it will log connect success
.
Create Table
As you know, the previous articles I used arrays to store data so when pressing f5 again will lose the newly added records, but in today’s post, we have connected to the database. then that is not a problem anymore. Now we will create a posts
table to replace the Mảng posts
that we used to in previous posts. First, I will declare a sql
statement as follows:
1 2 3 4 5 | <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token double-quoted-string string">"create table posts"</span> <span class="token operator">+</span> <span class="token double-quoted-string string">" (id int not null AUTO_INCREMENT,"</span> <span class="token operator">+</span> <span class="token double-quoted-string string">" title varchar(225),"</span> <span class="token operator">+</span> <span class="token double-quoted-string string">" PRIMARY KEY (id) )"</span> <span class="token punctuation">;</span> |
create a posts table with 2 fields of id
int, auto increment, primary key, and title
field has varchar data type. Next just need to trace the function to run the other sql.
1 2 3 4 5 6 7 8 9 10 | conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">throw</span> err <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token double-quoted-string string">"Create table success"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> |
Create table success
on the terminal shows that the Create table success
has been created successfully
Conclude
So in this article I have introduced to you how to install mysql on ubuntu and how to connect mysql to nodejs
. In the next post, I will perform the functions of data editing, edit delete table, and edit new added functions in the previous post. The article is somewhat flawed, we hope you to comment below for more, if you find the article useful, give yourself an upvote. Click follow to be able to follow my latest posts. Again thank you !!!