Shell操作MySQL

Shell修改MySQL初始密码,开启远程登录,授权远程登录用户

1、修改初始密码:
mysqladmin -uroot -pvyCE2UwSaqx3xofR password "123456"
mysqladmin -u root -proot password 123456
2、开启远程登录,授权远程登录用户:
mysql -uroot -proot -e "grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;"
3、使授权立即生效:
mysql -uroot -proot -e "flush privileges;"

4、使用-e进行参数传递,适合简单的命令,如
mysql -uuser -ppasswd -e "create database db;"

直接用命令导入:
mysql -uuser -ppasswd < test.sql

===============================================

允许root登录

vi /etc/ssh/sshd_config

将PermitRootLogin值改yes

允许不输入密码登录

将PermitEmptyPasswords yes前面的#号去掉

重启服务:service sshd restart(/etc/initd.d/sshd restart)

======================================================

mysql启动关闭shell脚本
 
#!/bin/sh    
mysql_port=3306  
mysql_username="root"  
mysql_password=""  
  
function_start_mysql()  
{  
printf "Starting MySQL...\n"  
/bin/sh /data/mysql/bin/mysqld_safe --defaults-file=/data/mysql/${mysql_port}/my.cnf 2>&1 > /dev/null &  
}  
  
function_stop_mysql()  
{  
printf "Stoping MySQL...\n"  
/data/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/mysql/mysql.sock shutdown  
  
}  
  
function_restart_mysql()  
{  
printf "Restarting MySQL...\n"  
function_stop_mysql  
sleep 5  
function_start_mysql  
}  
  
function_kill_mysql()  
{  
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port}  | awk '{printf $2}')  
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port}  | awk '{printf $2}')  
}   
if [ "$1" = "start" ]; then  
function_start_mysql  
elif [ "$1" = "stop" ]; then  
function_stop_mysql  
elif [ "$1" = "restart" ]; then  
function_restart_mysql  
elif [ "$1" = "kill" ]; then  
function_kill_mysql  
else  
printf "Usage: /data/mysql/${mysql_port}/mysql  
  
{start|stop|restart|kill}\n"  
fi  



你可能感兴趣的:(Shell操作MySQL)