1.官网下载mysql5.6版本:MySQL :: Download MySQL Community Server (Archived Versions) (5.6.49)
先卸载系统自带的Mariadb,
rpm -qa | grep mariadb
rpm -e --nodeps 文件名
如果之前有安装,找到所有mysql目录并删除:find / -name mysql|xargs rm -rf
2.将下载后的文件上传到linux系统文件夹:/usr/opt/
执行解压命令:tar zxvf mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz mysql
3.添加系统mysql组和mysql用户组:groupadd mysql ; useradd -r -g mysql mysql ;
4.开始安装数据库
删除/etc下的my.cnf,如无此文件,可以跳过此步骤
新建/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/opt/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/opt/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
进入安装mysql软件目录:
cd /usr/opt
修改当前目录拥有者为mysql用户:
chown -R mysql:mysql mysql
然后创建文件夹并赋予权限
mkdir /var/lib/mysql
mkdir /var/lib/mysql/mysql
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/mysql
yum install libaio* -y
./scripts/mysql_install_db --user=mysql
chown -R mysql:mysql data
chmod 755 /etc/my.cnf
设置开机自动启动服务的脚本
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
./scripts/mysql_install_db --user=mysql
增加mysqld服务控制脚本执行权限
chmod +x /etc/rc.d/init.d/mysqld
#将mysql服务加入到系统服务中
chkconfig --add mysqld
检查服务是否生效
chkconfig --list mysqld
看到3、4、5状态为开或者为 on 则表示成功。如果是 关或者 off
则执行:chkconfig --level 345 mysqld on
(
遇到问题->
-bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory
执行命令:yum -y install perl perl-devel
FATAL ERROR: please install the following Perl modules before executing /usr/bin/mysql_install_db:
Data::Dumper
执行命令:yum -y install autoconf 或 yum install -y perl-Data-Dumper
)
5.启动mysql服务:
service mysqld start
systemctl restart mysqld.service
vi /etc/profile
末尾加
export MYSQL_HOME="/usr/opt/mysql"
export PATH="$PATH:MYSQL_HOME/bin"
执行:source /etc/profile
开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=3306/udp --permanent
firewall-cmd --reload
创建软连接:
ln -s /usr/opt/mysql/bin/mysql /usr/bin/mysql
修改用户密码
./mysqld_safe --skip-grant-tables (在mysql/bin执行)
进入mysql
mysql -uroot -p
show databases;
use mysql;
show tables;
desc user;
select USER,HOST,Password from user;
delete from user where user = '';
update user set password=password('root') where user='root' and host='localhost';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
flush privileges;
退出
service mysqld stop
service mysqld start