您的位置:首页 > 运维架构 > Linux

CentOS7下安装MySql5.6.26

2015-09-28 22:56 891 查看
最近新换了工作,新的公司项目中服务器用的是Linux,虽然以前有自学过,但是都没有用到正式的环境中,为了能更快的融入新项目、新环境,就利用空闲时间自己鼓捣公司里面的东西,首先就从最基本的如何安装数据库开始!

环境准备

1.虚拟机环境下的Linux或者说服务器Linux,根据自身情况,博主是自身学习用所以就选择了虚拟机下的CentOS7的Linux环境。

2.MySql安装包下载,

ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.6/mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz

也可以到MySQL的官网进行下载或者说自行搜索,博主最后也提供了下载链接。

3.在/usr/local/下创建mysql目录并添加mysql用户和组

groupadd mysql
useradd -r -g mysql mysql


安装

1.解压

tar -xzf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz


注:安装目录需要设置到解压目录,否则报以下错误

FATAL ERROR: Could not find ./bin/my_print_defaults
If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.
If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.


or

FATAL ERROR: Could not find my-default.cnf
If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.
If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.


2.将解压的目录放到/usr/local下改为mysql或添加软连接,如果存在mysql目录,那就将解压目录下的文件放到/usr/local/mysql/目录下

mv mysql-5.6.26-linux-glibc2.5-x86_64 /usr/local/mysql
or

ln -s  /www/mysql-5.6.26-linux-glibc2.5-x86_64 /usr/local/mysql


3.修改mysql目录及子文件属主和属组

chown -R mysql:mysql
4000
mysql


4.进入mysql目录并安装

cd /usr/local/mysql/
scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql


5.如果中间没有错误的话那就恭喜你安装成功,进入到 /usr/local/mysql/support-files/ 目录,用

./mysql.server start


启动数据库。

开机自启动MySQL服务

1.CentOS7下用了systemctl替换了 service命令来管理开机自启动服务

参考:redhat文档:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Services.html#sect-Managing_Services_with_systemd-Services-List
查看全部服务命令:

systemctl list-unit-files --type service

查看服务

systemctl status name.service

启动服务

systemctl start name.service

停止服务

systemctl stop name.service

重启服务

systemctl restart name.service增加开机启动

systemctl enable name.service

删除开机启动

systemctl disable name.service

其中.service 可以省略。

2.MySQL增加启动参数

2.1增加mysql.service

在/usr/lib/systemd/system目录下增加mysql.service,目录必须是绝对目录。

[Unit]
Description=MySql
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/mysql/data/mysql.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/usr/local/mysql/support-files/mysql.server restart
ExecStop=/usr/local/mysql/support-files/mysql.server stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[unit]配置了服务的描述,规定了在network启动之后执行。[service]配置服务的pid,服务的启动,停止,重启。[install]配置了使用用户。

2.2使用mysql.service

#配置开机启动
systemctl enable mysql
#启动mysql
systemctl start mysql
#停止mysql
systemctl stop mysql
#重启mysql
systemctl restart mysql


安装过程中遇到的一些问题

1.perl依赖,安装perl
wget http://www.cpan.org/src/5.0/perl-5.22.0.tar.gz tar -zxvf perl-5.22.0.tar.gz
cd perl-5.22.0/
./Configure -des -Dprefix=$HOME/localperl
make
make test
make install
如果安装成功后还是报perl依赖的错误,那就安装perl-Module-Install.noarch这个模块
yum install -y perl-Module-Install.noarch
其他发行版本请自行搜索安装命令

2. ERROR 1045 (28000): Access denied for user ''@'localhost' (using password: NO)

2.1.关闭mysql

# /usr/local/mysql/support-files/mysql.server stop

2.2.屏蔽权限
# mysqld_safe --skip-grant-table

   屏幕出现: Starting demo from .....
2.3.新开起一个终端输入
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
mysql> \q

这样就好了。

3.当在shell下用mysql -uroot -p 连接MySql时,报 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'的错误,或者说是关于mysql.sock错误,解决思想就是看那个目录有没有那个文件或者说所在的目录是否属于mysql用户组,如果是说/tmp/下缺少那个配置,网上有人说可以改配置文件,但是博主修改配置文件不起效果,所以用了另一种添加软连接的方法解决了此问题。
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock


4.还有个错误信息就是/usr/local/mysql下的配置文件跟系统配置文件 /etc/my.cnf 有冲突,导致mysql服务启动不了,这时候只要删除系统配置文件就可以了,
rm -rf /etc/my.cnf
具体的错误信息如果以后碰到了再贴上来。

附件的源码包下载

链接地址   http://pan.baidu.com/s/1pJqvIfp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: