Centos7安装配置MySQL5.7
一:安装前准备:
1.1检查linux版本:cat /etc/system-release
CentOS Linux release 7.6.1810 (Core)
1.2查看系统是否安装MySQL:
rpm -qa | grep mysql
如果有安装则删除:
rpm -e --nodeps softfullname
1.3删除mariadb:
rpm -qa | grep mariadb
rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64
报错:
错误:依赖检测失败:
libmysqlclient.so.18()(64bit) 被 (已安裝) postfix-2:2.10.1-7.el7.x86_64 需要
libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) postfix-2:2.10.1-7.el7.x86_64 需要
不检测依赖强制删除:
rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
1.4关闭selinux:
getenforce
如果输出为:Enforcing
则需要关闭:
临时关闭selinux:
setenforce 0
永久关闭selinux:
vim /etc/selinux/config
SELINUX=disabled
reboot后生效
1.5检查是否安装libaio:
rpm -qa | grep libaio
yum search libaio
yum -y install libaio
二:官网下载:
2.1下载安装包:
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
选择版本
Linux - Generic
Linux - Generic (glibc2.5)(x86,64-bit)
Compressed TAR Archive
2.2上传文件到centos7
解压文件:
tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
移动文件到路径:
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
建议用此命令移动:
mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
cp -r mysql /usr/local/
三:安装mysql
3.1建立组和用户:
groupadd mysql
useradd -r -g mysql mysql
给mysql数据库创建专有用户,该用户只能访问mysql目录,不能访问系统其它目录
另外不建议直接用root初始化mysql,否则连接mysql时会报错:[ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
3.2创建data:
cd /usr/local/mysql/
mkdir data
cd /usr/local/
chown -R mysql:mysql mysql/
3.3初始化mysql:
cd /usr/local/mysql/bin/
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US
2019-01-15T01:21:28.631674Z 1 [Note] A temporary password is generated for root@localhost: uMQO;syZJ9q=
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2019-01-15T05:31:44.610567Z 1 [Note] A temporary password is generated for root@localhost: SXrGkn=;e1-d
记住临时密码
如果忘记密码或者想重新初始化,可以先将mysql/data目录中文件删除,然后再执行初始化命令
3.4修改配置文件:
vim /etc/my.cnf
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 socket=/var/lib/mysql/mysql.sock [mysqld] #skip-name-resolve #设置3306端口 port = 3306 socket=/var/lib/mysql/mysql.sock # 设置mysql的安装目录 basedir=/usr/local/mysql # 设置mysql数据库的数据的存放目录 datadir=/usr/local/mysql/data # 允许最大连接数 max_connections=200 # 服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf8 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB #lower_case_table_name=1 max_allowed_packet=16M
四:配置mysql:
4.1设置开机启动:
复制脚本到资源目录: cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
增加 mysqld 服务控制脚本执行权限: chmod +x /etc/rc.d/init.d/mysqld
将 mysqld 服务加入到系统服务: chkconfig --add mysqld
检查mysqld服务是否已经生效: chkconfig --list mysqld
命令输出类似下面的结果:
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用 service 命令控制 mysql 的启动和停止。
查看启动项:chkconfig --list | grep -i mysql
删除启动项:chkconfig --del mysql
启动 mysqld:service mysqld start
报错处理:
如报错:mysqld[3559]: /etc/rc.d/init.d/mysqld:行239: my_print_defaults: 未找到命令
拷贝文件到 /usr/bin
cp /usr/local/mysql/bin/my_print_defaults /usr/bin
如报错:mysqld[3683]: /etc/rc.d/init.d/mysqld: 第 259 行:cd: /user/local/mysql: 没有那个文件或目录
mysqld[3683]: Starting MySQL ERROR! Couldn't find MySQL server (/user/local/mysql/bin/mysqld_safe)
vim /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
mysqld_pid_file_path=/data/mysql
4.2添加环境变量:
vim /etc/profile
添加:
export PATH=$PATH:/usr/local/mysql/bin
export PATH
执行,使其生效:
source /etc/profile
查看是否生效:
echo $PATH
或者添加软连接 ln -s /usr/local/mysql/bin/mysql /usr/bin
五:登陆MySQL:
mysql -uroot -p
修改密码:
SET PASSWORD = PASSWORD("xxxx");
如登陆报错:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
建立软连接:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
删除软连接
rm -rf mysql.sock
不可写成,会删除链接文件
rm -rf mysql.sock/
六:添加防火墙策略:
感谢:
https://blog.csdn.net/vipbupafeng/article/details/80271089
https://www.cnblogs.com/zero-gg/p/8875598.html
https://blog.csdn.net/z13615480737/article/details/80019881