通過shell腳本對mysql的增刪改查及my.cnf的配置
shell操作mysql
1.獲取mysql默認(rèn)密碼
新安裝的mysql,密碼是默認(rèn)密碼
#!/bin/bash# STRING:獲取mysql默認(rèn)密碼的一段字符串# 例如:A temporary password is generated for root@localhost: xxxxxx# PASSWORD:將獲取到的STRING進(jìn)行截取,獲取localhost:右邊的默認(rèn)密碼# 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腳本進(jìn)行連接,會(huì)提示在命令行輸入密碼不安全。
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語句'# 因?yàn)樵趍y.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)文章:
1. Microsoft Office Access隱藏和顯示字段的方法2. SQL Server自動(dòng)生成日期加數(shù)字的序列號3. 關(guān)于Sql server數(shù)據(jù)庫日志滿的快速解決辦法4. SQL Server數(shù)據(jù)庫連接查詢和子查詢實(shí)戰(zhàn)案例5. Access創(chuàng)建一個(gè)簡單MIS管理系統(tǒng)6. 關(guān)于SQL server中字段值為null的查詢7. SQL語句中的ON DUPLICATE KEY UPDATE使用8. How to access eclipse workspace?9. DB2 9(Viper)快速入門10. Microsoft Office Access凍結(jié)字段的方法
