久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

通過shell腳本對mysql的增刪改查及my.cnf的配置

瀏覽:5日期:2023-10-01 10:03:43

shell操作mysql

1.獲取mysql默認密碼

新安裝的mysql,密碼是默認密碼

#!/bin/bash# STRING:獲取mysql默認密碼的一段字符串# 例如:A temporary password is generated for root@localhost: xxxxxx# PASSWORD:將獲取到的STRING進行截取,獲取localhost:右邊的默認密碼# shellcheck disable=SC2006STRING=`grep 'temporary password' /var/log/mysqld.log`PASSWORD=${STRING#*localhost: }

若已經(jīng)修改了密碼的

#!/bin/bash# shellcheck disable=SC2006PASSWORD='你的密碼'

2.修改my.cnf文件

原因:在mysq5.6還是5.7以上,使用如下的shell腳本進行連接,會提示在命令行輸入密碼不安全。

mysql -u root -pPASSWORD -e 'xxxxxx'

解決方法:使用sed命令在my.cnf文件中添加如下字段

[client]user=rootpassword=xxxxxx

shell腳本:

# 我的my.cnf文件在/etc/my.cnf下,不相同的可以自己去找找# sed -i ’第幾行 添加的內(nèi)容’ 指定的文件sed -i ’1i [client]’ /etc/my.cnfsed -i ’2i user=root’ /etc/my.cnfsed -i ’3i password=xxxxxx’ /etc/my.cnf

3.shell創(chuàng)建mysql數(shù)據(jù)庫

# SQL語句DATABASE_SQL='CREATE DATABASE IF NOT EXISTS test'# mysql -u 用戶名 -e 'sql語句'# 因為在my.cnf中配置了密碼,所以不用寫密碼了mysql -u root -e '${DATABASE_SQL}'

4.shell創(chuàng)建mysql表

# sql語句TEST_SQL='CREATE TABLE IF NOT EXISTS test ( id varchar(20) NOT NULL, text varchar(20) NOT NULL) ENGINE=InnoDB'# mysql -u 用戶名 -D '數(shù)據(jù)庫名' -e 'sql語句'mysql -u root -D 'test' -e '${TEST_SQL}'

5.shell添加數(shù)據(jù)

# sql語句INSERT_SQL='insert into test values (’123’, ’test’)'mysql -u root -D 'test' -e '${INSERT_SQL}'

6.shell刪除數(shù)據(jù)

DELETE_SQL='delete from test where id=’123’'mysql -u root -D 'test' -e '${DELETE_SQL}'

7.shell修改數(shù)據(jù)

UPDATE_SQL='update test set text=’你好’ where id=’123’'mysql -u root -D 'test' -e '${UPDATE_SQL}'

8.shell查找數(shù)據(jù)

SELECT_SQL='select id, text from test where id=’123’'mysql -u root -D 'test' -e '${SELECT_SQL}'

9.shell修改數(shù)據(jù)庫密碼

# mysql5.7之前SQL='update mysql set password=password('新密碼') where user=’root’'# mysql5.7及以后SQL='update mysql set authentication_string=password('新密碼') where user=’root’'# flush privileges:刷新mysql -u root -D 'mysql' -e '${SQL};flush privileges'

到此這篇關(guān)于通過shell腳本對mysql的增刪改查及my.cnf的配置的文章就介紹到這了,更多相關(guān)shell腳本mysql增刪改查內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

相關(guān)文章:
主站蜘蛛池模板: 安远县| 巴彦县| 宾川县| 红桥区| 漳州市| 仁怀市| 龙山县| 吉木乃县| 廉江市| 新田县| 隆安县| 周口市| 曲阜市| 扶绥县| 昌邑市| 文登市| 兴和县| 江门市| 庆安县| 龙陵县| 黑山县| 钟山县| 延寿县| 梁平县| 山阳县| 诏安县| 孟津县| 洱源县| 日喀则市| 莎车县| 无极县| 建德市| 微山县| 延吉市| 绥化市| 同仁县| 中江县| 乐业县| 张家界市| 利辛县| 明光市|