Centos7.4下安装mysql-5.6.41

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7.4下安装mysql-5.6.41相关的知识,希望对你有一定的参考价值。

Centos7.4下安装mysql-5.6.41二进制包

1、下载
mkdir /data/sql
cd /data/sql

wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

2、查询是否有安装过mysql
rpm -qa | grep mysql

若有的话,卸载低版本的MySQL
rpm -e --nodeps mysql*

卸载MariaDB
查看当前安装的mariadb包:# rpm -qa | grep mariadb
都卸载掉:# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

3、重命名MySQL5.6.41名称
mv mysql-5.6.41-linux-glibc2.12-x86_64 /data/mysql

因为我想mysql放在一个挂载盘下/data (属于挂载盘的目录)
但是basedir和datadir还是用/usr/local/mysql
就做个软连接

ln -s /data/mysql /usr/local/

4、创建MySQL用户及用户组

groupadd mysql
useradd -r -g mysql mysql

chown -R mysql.mysql /data/mysql

5、安装MySQL
先安装一些库文件
yum –y install perl perl-devel
yum -y install autoconf
yum install libaio* -y

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

#####
问题1:若有提示:FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:

解决方法 :安装autoconf库
命令:yum -y install autoconf

#####
问题2:Installing MySQL system tables.../usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
原因:缺少libaio库文件
解决方法:yum install libaio* -y

6、/etc/profile文件里添加下面MySQL环境变量
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin

输入命令使其生效
source /etc/profile

7、复制my.cnf
cp /data/mysql/support-files/my-default.cnf /etc/my.cnf

添加一下内容:
具体参数根据你实际情况来, 自己调优

###############
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

socket=/usr/local/mysql/mysql.sock

port=3306
user=mysql

max_allowed_packet = 1024M
net_read_timeout = 60
net_retry_count = 10
net_write_timeout = 60
connect_timeout=30

max_connections=2000
skip-name-resolve=1

innodb_log_buffer_size = 32M
innodb_log_file_size = 1024M

#slow_query_log = ON
#slow_query_log_file = /usr/local/mysql/data/slow.log
#long_query_time = 1

character_set_server = utf8

#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
socket=/usr/local/mysql/mysql.sock

[client]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
max_allowed_packet=1024M

#################

8、初始化MySQL服务
cd /usr/local/mysql/
[[email protected] mysql]# mysqld_safe
180930 12:00:53 mysqld_safe Logging to ‘/var/log/mysqld.log‘.
180930 12:00:53 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
180930 12:01:13 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

9、设置开机启动服务

cd /usr/local/mysql/
cp support-files/mysql.server /etc/rc.d/init.d/mysqld

chkconfig --add mysqld
chkconfig mysqld on

开机启动服务
chkconfig --add mysqld
chkconfig mysqld on

10、启动MySQL服务
/etc/init.d/mysqld start

问题: [ERROR] Fatal error: Can‘t open and lock privilege tables: Table ‘mysql.user‘ doesn‘t exist
解决方法因为目录没有赋权MySQL导致,解决重新执行就可以
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

若用了/ect/my.cnf来控制参数设置,那/data/mysql/下的my.cnf需要删除掉,不然配置会优先/data/my.cnf

11、修改密码及赋权

方法1: 用SET PASSWORD命令
首先登录MySQL。
格式:mysql> set password for 用户名@localhost = password(‘新密码‘);
例子:mysql> set password for [email protected] = password(‘123‘);

方法2:用mysqladmin
格式:mysqladmin -u用户名 -p旧密码 password 新密码
例子:mysqladmin -uroot -p123456 password 123

方法3:用UPDATE直接编辑user表
首先登录MySQL。
mysql> use mysql;
mysql> update user set password=password(‘123‘) where user=‘root‘ and host=‘localhost‘;
mysql> flush privileges;

12、赋权
#指定IP、指定账号、密码访问数据库
mysql> GRANT ALL PRIVILEGES ON . TO [username]@"[ip]" IDENTIFIED BY "[password]";
?
#指定IP、指定账号、密码访问数据库-查询权限
mysql> grant select on 数据库.* to 用户@IP identified by ‘密码‘;

#刷新
mysql> flush privileges;

以上是关于Centos7.4下安装mysql-5.6.41的主要内容,如果未能解决你的问题,请参考以下文章

mysql 5.6.41编译安装详细步骤

mysql5.6安装

Mac下Mysql配置

MySQL 5.6 多实例部署

关于windows下安装mysql数据库出现中文乱码的问题

CentOS7.4 源码编译安装LNMP