Linux下安裝Mysql相對windows較為復(fù)雜搔弄,折騰了2天多,終于安裝和配置完成丰滑,簡單記錄安裝和配置過程顾犹,如下:
Ⅰ.操作環(huán)境說明:
Linux:CentOS6.5_64 bit
Mysql:mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
Ⅱ.安裝過程:
安裝mysql前,先用 rpm -qa | grep mysql 查看系統(tǒng)自帶的mysql-libs;然后用 rpm -e --nodeps mysql-libs-XXX-el6-xxx;刪除自帶的mysql庫; 網(wǎng)上安裝的貼子多不勝數(shù)褒墨,安裝的文件夾也很亂炫刷,在此統(tǒng)一說明,Mysql默認(rèn)安裝環(huán)境:/usr/local/mysql;無特殊情況不建議更換郁妈,安裝至其它自定義目錄浑玛,還要修改相關(guān)的配置文件,作為新手噩咪,我安裝至默認(rèn)目錄:
①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
#執(zhí)行后可通過 service mysql -u user -p 登陸mysql
shell> chkconfig /etc/init.d/mysql.server on #設(shè)置mysql.server開機(jī)啟動(dòng)
②mysql常用命令:
ps -A | grep mysql #查看mysql服務(wù)進(jìn)程, 得到 mysql進(jìn)程的PID;
kill -9 PID1 PID2...PIDn #終止mysql進(jìn)程;
/etc/init.d/mysql.server start/stop/restart #啟動(dòng)/停止/重啟mysql服務(wù):
service mysql -u root -p #登錄mysql:
③mysql初次登錄:
bin/mysqladmin -u root password ' password' #設(shè)置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文件可以保存用戶的自定義設(shè)置,my.cnf文件存在與多個(gè)位置胃碾;安裝過程中生成的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文件的讀取順序涨享,后面的文件配置會(huì)覆蓋前面的文件配置;
Ⅲ.設(shè)置mysql默認(rèn)字符集:
查看字符集: 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用’#‘注釋掉;此時(shí)书在,mysql 默認(rèn)字符集已全改為utf8
Ⅳ.安裝&配置過程中常見錯(cuò)誤:
①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!
解決辦法:啟動(dòng)mysql.server服務(wù)
ps -A |grep mysql #得到mysql 的Pid
kill -9 Pid1 Pid2 #終止mysql進(jìn)程;
/etc/init.d/mysql.server restart #啟動(dòng)mysql.server服務(wù)
②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執(zhí)行失敗
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;用‘#’注釋掉