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;用‘#’注释掉
网友评论