


Shell handles mysql addition, deletion, modification and query
Introduction
I have been doing a task these days to compare the data in two data tables. I wrote a version using PHP yesterday. However, considering that some machines do not have PHP or PHP does not compile the mysql extension, I cannot use the mysql series of functions and scripts. It is invalid. Today I will write a shell version so that it can run on all Linux series machines.
How does shell operate mysql?
Shell operating mysql is actually to execute statements through parameters through the mysql command, which is the same as in other programs. Take a look at the following parameters:
<code>-e, --execute=name Execute command and quit. (Disables --force and history file.)</code>
So we can execute the statement through mysql -e, like the following:
<code>mysql -hlocalhost -P3306 -uroot -p123456 $test --default-character-set=utf8 -e "select * from users"</code>
After execution, the following results are returned:
Operate mysql in shell script
Export data
<code>MYSQL="mysql -h192.168.1.102 -uroot -p123456 --default-character-set=utf8 -A -N" #这里面有两个参数,-A、-N,-A的含义是不去预读全部数据表信息,这样可以解决在数据表很多的时候卡死的问题 #-N,很简单,Don't write column names in results,获取的数据信息省去列名称 sql="select * from test.user" result="$($MYSQL -e "$sql")" dump_data=./data.user.txt >$dump_data echo -e "$result" > $dump_data #这里要额外注意,echo -e "$result" > $dump_data的时候一定要加上双引号,不让导出的数据会挤在一行 #下面是返回的测试数据 3 吴彦祖 32 5 王力宏 32 6 ab 32 7 黄晓明 33 8 anonymous 32</code>
Insert data
<code>#先看看要导入的数据格式,三列,分别是id,名字,年龄(数据是随便捏造的),放入data.user.txt 12 tf 23 13 米勒 24 14 西安电子科技大学 90 15 西安交大 90 16 北京大学 90 #OLF_IFS=$IFS #IFS="," #临时设置默认分隔符为逗号 cat data.user.txt | while read id name age do sql="insert into test.user(id, name, age) values(${id}, '${name}', ${age});" $MYSQL -e "$sql" done</code>
Output results
<code>+----+--------------------------+-----+ | id | name | age | +----+--------------------------+-----+ | 12 | tf | 23 | | 13 | 米勒 | 24 | | 14 | 西安电子科技大学 | 90 | | 15 | 西安交大 | 90 | | 16 | 北京大学 | 90 | +----+--------------------------+-----+</code>
Update data
<code>#先看看更新数据的格式,将左边一列替换为右边一列,只有左边一列的删除,下面数据放入update.user.txt tf twoFile 西安电子科技大学 西军电 西安交大 西安交通大学 北京大学 cat update.user.txt | while read src dst do if [ ! -z "${src}" -a ! -z "${dst}" ] then sql="update test.user set name='${dst}' where name='${src}'" fi if [ ! -z "${src}" -a -z "${dst}" ] then sql="delete from test.user where name='${src}'" fi $MYSQL -e "$sql" done</code>
Output result:
<code>+----+--------------------------+-----+ | id | name | age | +----+--------------------------+-----+ | 12 | twoFile | 23 | | 13 | 米勒 | 24 | | 14 | 西军电 | 90 | | 15 | 西安交通大学 | 90 | +----+--------------------------+-----+</code>
The copyright of this article belongs to the author iforever []. Any form of reprinting is prohibited without the consent of the author. After reprinting the article, the author and the original text link must be given in an obvious position on the article page.
The above introduces the shell processing of mysql addition, deletion, modification, and query, including aspects of the process. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
