【MySQL】MySQLのrootパスワードのリセットとMySQLのデフォルト文字セットの変更

3264 ワード

Rails開発で初めてMySQLデータベースを使用した結果、rootパスワードもどのようにインストールするか分からず、rootにログインできなかったので、パスワードをリセットすることを考え、ネット上で多くの方法を探しました.以下は最も頼りになります.
新しいスクリプトは次のとおりです.
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    printf "Error: You must be root to run this script!
" exit 1 fi echo "=========================================================================
" printf "Reset MySQL root Password for LNMP , Written by Licess
" printf "=========================================================================
" printf "LNMP is a tool to auto-compile & install Nginx+MySQL+PHP on Linux
" printf "This script is a tool to reset mysql root password for lnmp
" printf "For more information please visit http://www.lnmp.org
" printf "
" printf "Usage: sh reset_mysql_root_password.sh
" printf "=========================================================================
" mysql_root_password="" read -p "(Please input New MySQL root password):" mysql_root_password if [ "$mysql_root_password" = "" ]; then echo "Error: Password can't be NULL!!
" exit 1 fi printf "Stoping MySQL...
" /etc/init.d/mysql stop printf "Starting MySQL with skip grant tables
" /usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 & printf "using mysql to flush privileges and reset password
" sleep 10 printf "update user set password = Password('$mysql_root_password') where User = 'root'
" /usr/bin/mysql -u root mysql << EOF update user set password = Password('$mysql_root_password') where User = 'root'; EOF reset_status=`echo $?` if [ $reset_status = "0" ]; then printf "Password reset succesfully. Now killing mysqld softly
" killall mysqld sleep 10 printf "Restarting the actual mysql service
" /etc/init.d/mysql start printf "Password successfully reset to '$mysql_root_password'
" else printf "Reset MySQL root password failed!
" fi

中には、以下のような手作業で修正する必要があるものもあります.
/usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &

マイコンピュータにコマンドを入力:whereis mysqld_safe 
結果は次のとおりです.
mysqld_safe:/usr/bin/mysqld_safe/usr/bin/X11/mysqld_safe/usr/share/man/man1/mysqld_safe.1.gz
だから個人の状況によって31行目を修正します.
もう1つは35行目で、私は実行します:whereis mysql、結果は以下の通りです.
mysql: /usr/bin/mysql /etc/mysql /usr/lib/mysql /usr/bin/X11/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz

MySQLのデフォルト文字セットについては、次のように変更します.
ここではlinuxシステムのみを考慮し、/etc/mysql/my.を見つけます.cnf、root権限を使用して開き、
以下の変更を行います.
           :

[client]
default-character-set=utf8

[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

変更後、mysqlのサービスを再起動し、サービスmysql restart
mysql>SHOW VARIABLES LIKE'character%'を使用します.データベース符号化がutf 8に変更されたことがわかりました.