Linux下安裝Mysql相對windows較為復雜,折騰了2天多,終于安裝和配置完成,簡單記錄安裝和配置過程,如下:
Ⅰ.操作環境說明:
Linux:CentOS6.5_64 bit
Mysql:mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
Ⅱ.安裝過程:
安裝mysql前,先用 rpm -qa | grep mysql 查看系統自帶的mysql-libs;然后用 rpm -e --nodeps mysql-libs-XXX-el6-xxx;刪除自帶的mysql庫; 網上安裝的貼子多不勝數,安裝的文件夾也很亂,在此統一說明,Mysql默認安裝環境:/usr/local/mysql;無特殊情況不建議更換,安裝至其它自定義目錄,還要修改相關的配置文件,作為新手,我安裝至默認目錄:
①Install MySQL binary distribution:
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar -xzvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz #解壓后直接更名為mysql
shell> cd mysql
shell> mkdir mysql-files
shell> chmod 770 mysql-files
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> ./scripts/mysql_install_db --user=mysql # Before MySQL 5.7.6 //安裝mysql
shell> chown -R root .
shell> chown -R mysql data mysql-files
shell> bin/mysqld_safe --user=mysql &
shell> cp support-files/mysql.server /etc/init.d/mysql.server
shell> cp bin/mysql /etc/init.d/mysql
#執行后可通過 service mysql -u user -p 登陸mysql
shell> chkconfig /etc/init.d/mysql.server on #設置mysql.server開機啟動
②mysql常用命令:
ps -A | grep mysql #查看mysql服務進程, 得到 mysql進程的PID;
kill -9 PID1 PID2...PIDn #終止mysql進程;
/etc/init.d/mysql.server start/stop/restart #啟動/停止/重啟mysql服務:
service mysql -u root -p #登錄mysql:
③mysql初次登錄:
bin/mysqladmin -u root password ' password' #設置root密碼
④安裝過程分析:
To start mysqld at boot time you have to copy support-files/mysql.server to the right place for
your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h hostname password 'new-password'
You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
New default config file was created as ./my.cnf and will be used by default by the server when you start it.
You may edit this file to change server settings
my.cnf文件很值得一談,Mysql5.7.6以后不再生成my.cnf文件,安裝包中也沒有了my-default.cnf文件,my.cnf文件可以保存用戶的自定義設置,my.cnf文件存在與多個位置;安裝過程中生成的my.cnf存在于/usr/local/mysql/my.cnf;
Default options are read from the following files in the given order:
/etc/my.cnf => /etc/mysql/my.cnf => /usr/local/mysql/etc/my.cnf => ~/.my.cnf
my.cnf文件的讀取順序,后面的文件配置會覆蓋前面的文件配置;
Ⅲ.設置mysql默認字符集:
查看字符集: show variables like 'character_%';
①修改my.cnf (/usr/local/mysql/my.cnf):
[client]
...
default_character_set=utf8
[mysqld]
...
character_set_server=utf8
[mysql]
...
default_character_set=utf8
②cp my.cnf /etc/my.cnf#將my.cnf拷貝至/etc下
③vi /etc/my.cnf
將最后一行sql_mode用’#‘注釋掉;此時,mysql 默認字符集已全改為utf8
Ⅳ.安裝&配置過程中常見錯誤:
①ERROR 2002 (HY000):
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)。
error: 'Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2)'。
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
解決辦法:啟動mysql.server服務
ps -A |grep mysql #得到mysql 的Pid
kill -9 Pid1 Pid2 #終止mysql進程;
/etc/init.d/mysql.server restart #啟動mysql.server服務
②ERROR 1045 (28000):
Access denied for user 'root'@'localhost' (using password: NO)
shell>> /etc/init.d/mysql stop
shell>> mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
shell>> mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> mysql> FLUSH PRIVILEGES;
mysql> quit
shell>> /etc/init.d/mysql restart
③ /etc/init.d/mysql restart執行失敗
Starting MySQL...The server quit without updating PID file [失敗]local/mysql/data/hostname.pid).
vi my.cnf
[mysqld]
...
default_character_set=utf8 改為 character_set_server=utf8
④service mysql -u root -p 登錄失敗
/etc/init.d/mysql: unknown variable'sql_mode=NO_ENGINE_SUBSTITUTI ON,STRICT_TRANS_TABLES'
將/etc/my.cnf 中最后一句 sql_mode=XXXXX;用‘#’注釋掉