ubuntu一键安装mysql

ubuntu一键安装mysql

Tags: Ubuntu

摘要

ubuntu一键安装mysql

一键安装mysql

wget -O mysql.sh https://coding.net/u/javacore/p/dev-env-shell/git/raw/master/reinstall_mysql.sh && bash mysql.sh

执行命令后,会设置root的密码为root,且只有127.0.0.1可链接。
如果需要远程链接,可以手动新增一个用户。

特别注意,这个命令会清除原有的数据库以及数据,请注意备份。
特别注意,字符编码是utf8mb4,数据库引擎是InnoDB。

手动新增用户

mysql -h127.0.0.1 -uroot -proot
grant all privileges on *.* to zhangxiaosan@'%' identified by 'password!' ;
flush privileges;
service mysql restart

新建数据库

CREATE DATABASE db_javacore DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

完整内容备份

## reinstall
# 


echo "rm mysql" ;

echo "first uninstall mysql";
apt-get update    ;
service mysql stop  ;
apt-get autoremove -y --purge mysql-server ;
apt-get remove -y mysql-server ;
apt-get autoremove -y mysql-server ;
apt-get remove -y mysql-common  ;

#echo "clean mysql files";

rm -rf /etc.my.cnf ; 
#txt=`find / -name mysql`
#for x in $txt
#do
#echo "rm -rf $x"
#rm -rf  $x ;
#done


#set auto password
debconf-set-selections <<< 'mysql-server mysql-server/root_password password root' ;
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root' ;

#begin install mysql

apt-get install -y mysql-server    ;
apt-get install -y mysql-client    ;
apt-get install -y libmysqlclient-dev ;


#set password root
mysql -uroot -proot <<EOF
drop database if exists test;
grant all privileges on *.* to root@'127.0.0.1' identified by 'root' ;
flush privileges;
exit
EOF

#set utf8mb4
myconf="/etc/mysql/my.cnf"
sed -i "s/bind-address/# &/" ${myconf}

line=`sed -n '/\[client\]/=' ${myconf} | tail -n1`
sed -i  "${line} adefault-character-set=utf8mb4" ${myconf}
line=`sed -n '/\[mysqld\]/=' ${myconf} | tail -n1`
sed -i  "${line} adefault-storage-engine =InnoDB  #MYISAM" ${myconf}
sed -i  "${line} acharacter_set_server=utf8mb4" ${myconf}


#resetart
service mysql restart