通過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. Window7安裝MariaDB數(shù)據(jù)庫及系統(tǒng)初始化操作分析2. MariaDB的安裝與配置教程3. SQLite教程(十二):鎖和并發(fā)控制詳解4. Centos7 下mysql重新啟動(dòng)MariaDB篇5. MariaDB性能調(diào)優(yōu)工具mytop的使用詳解6. centos 7安裝mysql5.5和安裝 mariadb使用的命令7. access不能打開注冊表關(guān)鍵字錯(cuò)誤處理方法(80004005錯(cuò)誤)8. SQL案例學(xué)習(xí)之字符串的合并與拆分方法總結(jié)9. SQLite 性能優(yōu)化實(shí)例分享10. MariaDB數(shù)據(jù)庫的外鍵約束實(shí)例詳解
