Sunday, October 13, 2024

Learn MySQL: Create, Update, and Delete Data in Database

https://www.tecmint.com/basic-mysql-commands

A database is a structured set of data stored electronically. The concept of a database was known to our ancestors even when there were no computers. However, creating and maintaining such databases was a very tedious job. For instance, in a manual database of 100 pages, searching for all employees whose salaries were less than 10,000 would have been quite difficult.

In today’s world, you cannot escape databases. Right now, millions of databases are working around the world to store and fetch data of every kind, whether it be strategic data, employee records, or web technologies.

Databases are often termed as backend processes because they are neither visible to end users nor do end users interact directly with the database. Instead, they work on frontend processes like PHP, VB, ASP.NET, etc., and ask the frontend to deal with the database in the backend.

There are several database servers and clients available, such as Oracle, MySQL, MySQLi, MariaDB, and MongoDB. The syntax for all of these is more or less the same. Mastering one means gaining control over most of them, and learning the queries of a database is both easy and enjoyable.

Let’s start with simple queries on databases. We will use MySQL, which comes bundled with most Linux distributions by default. You can install it manually from the repository if it is not installed by default in your case.

A database query is a simple piece of code sent to the database to obtain custom and refined results as required.

Install MySQL Database in Linux

Use the “yum” or “apt” package manager to install the MySQL database.

sudo yum install mysql mysql-client mysql-server  (on Yum-based systems)
sudo apt install mysql mysql-client mysql-server  (on Apt-based systems)

Once installed, start the MySQL database service with:

sudo systemctl start mysqld
Or
sudo systemctl start mysql

Installing a fresh copy of the MySQL database will take you to a configuration step where you will be asked to set up an root password and answer questions regarding security settings.

sudo mysql_secure_installation

Once you finish installing and securing the server, go to your MySQL prompt.

sudo mysql -u root -p
MySQL Shell Prompt
MySQL Shell Prompt

Now, executing queries at this prompt is both educational and enjoyable.

Create a MySQL Database

Create a database named “tecmint“.

create database tecmint;

Note: The message indicates that the query was successful, meaning the database is created.

You can verify your newly created database by running:

show databases; 

Notice your database in the output below.

Create a MySQL Database
Create a MySQL Database

Create Tables in MySQL Database

Now you need to select the database to work on:

use tecmint;

Here we will create a table called “minttec” with three fields:

CREATE TABLE minttec (
    id INT(3), 
    first_name VARCHAR(15), 
    email VARCHAR(20)
);

Note: The above query returns “OK“, indicating that the table was created without any errors.

To verify the table, run the following query:

show tables; 

You can view the columns you created in the “minttec” table as follows:

Create Tables in MySQL Database
Create Tables in MySQL Database

Anyway, let me explain the types of declarations and their meanings.

  • INT is an Integer.
  • VARCHAR is a character type with a variable length as defined. The value after the type indicates the maximum length of the field in which it can store data.

Add Columns in MySQL Database

Now, we need to add a column named ‘last_name‘ after the column ‘first_name‘:

ALTER TABLE minttec ADD last_name VARCHAR(20) AFTER first_name;

Verify the change in your table:

show columns from minttec; 
Add Column in MySQL Database
Add Column in MySQL Database

Now we will add a column named ‘country‘ to the right of the email field:

ALTER TABLE minttec ADD country VARCHAR(15) AFTER email; 

Verify the column addition:

show columns from minttec; 
Verify Column in MySQL Database
Verify Column in MySQL Database

Insert Values into Fields of MySQL Database

Now let’s insert values into the fields:

INSERT INTO minttec VALUES ('1', 'Ravi', 'Saive', 'raivsaive@xyz.com', 'India');

Now let’s insert multiple values at once into the table.

INSERT INTO minttec (id, first_name, last_name, email, country) VALUES 
  ('2', 'Narad', 'Shrestha', 'narad@xyz.com', 'India'), 
  ('3', 'user', 'singh', 'user@xyz.com', 'Aus'), 
  ('4', 'tecmint', 'com', 'tecmint@gmail.com', 'India');

Verify the inserted values:

select * from minttec; 
Verify Values in MySQL Database
Verify Values in MySQL Database

The values have been inserted successfully.

Update Values in MySQL Table

Now, how about changing the last name of the user whose first name is “Narad“?

UPDATE minttec SET last_name = 'Shrestha' WHERE first_name = 'Narad';

Check to verify the changes.

select * from minttec; 
Update Values in Table
Update Values in Table

Delete Values from MySQL Table

What about deleting a row from the table? For example, let’s delete the last entry of the user whose first name is “tecmint“.

DELETE FROM minttec WHERE first_name = 'tecmint';

Now, check the entries after the deletion.

select * from minttec; 
Delete Values in Table
Delete Values in Table

Now, as you can see, the user “tecmint” has been deleted successfully.

Rename Table in MySQL

To rename a table in MySQL, you can use the RENAME TABLE statement. Let’s say you want to rename the table minttec to users. You would run the following command.

RENAME TABLE minttec TO users;

After renaming the table, you can verify the change by listing the tables in your current database:

SHOW TABLES;
Rename Table in Database
Rename Table in Database

Backup a Database

To back up a MySQL database, you can use the mysqldump command, which creates a logical backup by generating a SQL script file containing all the commands to recreate the database.

mysqldump -u root -p tecmint > tecmint_backup.sql

You can verify that the backup file was created by listing the files in your current directory:

ls -l tecmint_backup.sql

Restore a Database

To restore a MySQL database from a backup file, you can use the mysql command.

mysql -u root -p tecmint < tecmint_backup.sql

Make sure the database you are trying to restore to already exists. If it doesn’t, you can create it using:

mysql -u root -p -e "CREATE DATABASE tecmint;"
Conclusion

You have now learned the basics of creating and manipulating a MySQL database, including creating a database, creating tables, inserting records, updating records, deleting records, and dropping tables. This foundation is essential for working with databases effectively.

 

What is Systemctl and How Should You Use It?

https://www.rosehosting.com/blog/what-is-systemctl

What is Systemctl and How Should You Use It?

What is Systemctl and how should you use it

Systemd is a popular init system that serves as a management tool for various systems within the Linux operating environment. Through systemd, we can perform essential administrative and maintenance functions, like managing system resources, controlling startup options, logging, and journaling. At the same time, systemctl is a command used to manage systemd services, which are currently standard on Linux.

Lennart Poettering is the person behind the development of systemd, a software engineer who now works at Microsoft. Lennart Poettering previously also created controversial programs, namely Avahi and PulseAudio.

Conventions

# – given commands should be executed with root privileges either directly as a root user or by use of sudo command
$ – given commands should be executed as a regular user

The init system is the first process after the Linux Kernel comes into action in the boot process. It initializes various device management, logging, and networking service. ‘service’ is a legacy command that is used in older versions of Linux/Unix systems. The tool is used to start, stop, and manage system services. It is still available in many modern systems, but most Linux distributions use systemctl now.

Systemd is indeed controversial for several reasons. Firstly, many people think that systemd doesn’t respect the Unix philosophy. The claim is that systemd is too complex and monolithic, making troubleshooting harder. Secondly, some criticisms of systemd state that systemd is not only an init system but also takes over many functions. For example, systemd tries to manage network, cron, fstab, syslog, etc. This means that systemd is not a program with a singular use but has multiple functions. Finally, systemd is criticized as its logging file isn’t text-based like Unix and Linux logs in common. Instead, it’s a binary log file.

Among the criticisms of systemd mentioned above, some are simply invalid. For example, systemd is not considered modular. Lennart Poettering answers such criticisms on his blog page entitled The Biggest Myths.

Most major Linux distributions like Ubuntu, AlmaLinux, and Gentoo now use systemd as their default init system. Debian was late implementing systemd in their release because many developers rejected it. As a result, several Debian developers who rejected systemd created a new Debian-based distribution, Debian without systemd, called Devuan. Devuan is Debian that still uses sysvinit.

After several years, most would agree that systemd is now a modern system — the opposite of sysvinit. Sysvinit should have disappeared from the init world because managing services through scripts is not easy and complex. Ubuntu once made a replacement for Sysvinit with Upstart but then abandoned it and switched to systemd. Systemd uses a configuration file called a unit. Creating a unit file is also easy because of its ini-style configuration.

systemd Commands

The following are several commands you can use to manage your services using systemd.

To restart a service, use systemctl restart. For example, restart nginx.

# systemctl restart nginx

To start, stop, and reload, replace restart with start, stop, or reload.

Systemctl can also start/stop/restart several services using a single command, for example:

# systemctl restart nginx mysql postfix

To enable service at start-up, use the following command:

# systemctl enable nginx

Alternatively, if instead, you wish to enable the service at start-up and start it right now, you would use:

# systemctl enable --now nginx

To disable service at start-up type

# systemctl disable apache2

If you need to see the systemd file of a service, you can type the following as an example:

# systemctl cat nginx

To restart the server, you should use:

# systemctl reboot

Shutting down the server is using the following command:

# systemctl poweroff

If you wish to view live logs, use:

# journalctl -f

For viewing logs of a service, you can use the following:

# journalctl -u mysql

You can use a command similar to the one below when looking for a specific date range. In this case, we view logs from August 29th to August 31st:

# journalctl --since "2024-08-29" --until "2024-08-31"

Finally, to view the most recent logs, you can use the following command:

# journalctl --since "3 hours ago"

As with the service command, systemctl is a very versatile command. It lets you enable, reload, start, stop, and check the status of services running on your Linux machine. Systemctl serves as an easy-to-use and powerful interface for configuring files in systemd. For example, it only takes one or two commands to disable a service at boot using systemctl.

Service and systemctl mainly have the same purpose. Many of the similarities between the two are intentionally inserted to allow users to make a smoother transition to systemctl. However, there are differences between the two to be aware of when starting work with the initialization system in Linux.

The most significant difference between service and systemctl is that they belong to different initialization systems. Service belongs to the classic Linux initialization process of SysVinit (System V Init). Systemctl belongs to systemd, the successor to SysVinit, and the modern initialization process is used on many Linux systems. Because service and systemctl are part of different initialization processes. They operate on initialization system files in different directories. Service works with the initialization system files found in /etc/init.d. Meanwhile, systemctl works with the initialization system files found in /lib/systemd.

Systemd starts services in parallel rather than serially, so its boot-up time is faster than SysVinit. It also offers slightly more sophisticated control of system daemons via a command-line interface (CLI).

Conclusion

That’s it all! You no longer need to ask what is systemctl and you now know how to use it.

If you have an active service with us, you don’t have to spend your precious time or stress about systemctl. You can contact our expert administrators through chat or by ticket. Our technical support department is available 24×7 and will take care of your request immediately.

If you liked this what is systemctl and how to use it post, please share it with your friends. Thanks.

Tags , , , , ,

 

How to Reset USB Device Using Command Line in Linux

https://www.tecmint.com/reset-usb-device-linux

How to Reset USB Device Using Command Line in Linux

Resetting a USB device from the Command Line Interface (CLI) can help resolve issues such as unresponsiveness or connection problems. This guide will walk you through the steps to reset a USB device using simple commands.

Step 1: Identify the USB Device in Linux

Before resetting the USB device, you must identify it by using the lsusb command, which lists all USB devices connected to your system.

lsusb

You’ll see a list of connected USB devices, something like this:

Find USB Drive in Linux
Find a USB Drive in Linux

Note the bus and device number of the USB device you want to reset (e.g., Bus 001 Device 004).

Step 2: Unmount the USB Device in Linux

If the USB device is mounted, you need to unmount it before resetting it by using the umount command followed by the device’s mount point.

You can use the df command to see where the USB device is mounted:

df -h

Look for your USB device in the output, which will usually looks like /media/username/device_name.

Find USB Device Mount Point
Find USB Device Mount Point

Now run the command below, replacing /media/username/device_name with the actual mount point:

sudo umount /media/username/device_name

Step 3: Reset the USB Device in Linux

After unmounting the device, you can reset it using the usbreset utility. If you don’t have usbreset installed, you can install it using the following appropriate command for your specific Linux distribution.

sudo apt install usbutils         [On Debian, Ubuntu and Mint]
sudo yum install usbutils         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/usbutils  [On Gentoo Linux]
sudo apk add usbutils             [On Alpine Linux]
sudo pacman -S usbutils           [On Arch Linux]
sudo zypper install usbutils      [On OpenSUSE]    
sudo pkg install usbutils         [On FreeBSD]

Once installed, you can reset the USB device using the following command by replacing Bus 001 Device 004 with your USB device’s bus and device number:

sudo usbreset /dev/bus/usb/001/004

Alternatively, you can use the following command, where <device_id> is the ID of the USB device (from the lsusb output):

sudo usbreset 090c:1000
Reset USB Device in Linux
Reset USB Device in Linux

Step 4: Remount the USB Device in Linux

After resetting the USB device, you can remount it. If your system does not automatically mount it, you can do it manually by running the following commands.

lsblk
sudo mount /dev/sdX1 /media/username/device_name

Replace /dev/sdX1 with your USB device’s identifier, which you can find using the lsblk command.

Mount USB Device in Linux
Mount USB Device in Linux

Finally, verify that the USB device is functioning properly by running the lsusb command. If it appears in the list, it is successfully reset and ready to use.

lsusb
Verify USB Device in Linux
Verify USB Device in Linux
Conclusion

Resetting a USB device from the Linux terminal is a straightforward process that can help resolve various issues. By following the steps outlined above, you can identify, unmount, reset, and remount your USB device without much hassle.