CentOS MySQL5.5编译安装

准备工作:

CentOS系统,MySQL安装包(也可在系统在线下载)

实验环境:腾讯云centos7.2 X64 , mysql-5.5.54

描述:源码下载目录/src/;安装目录/usr/local/mysql/;目录可根据自己及的需求自定义

一、远程连接到Linux服务器,

二、创建mysql用户及所属mysql组不允许登录,在线下载mysql源码包,并解压,

  • 创建mysql用户及组不允许登录,可以使用cat /etc/passwd命令查看下用户及组是否创建成功;

    groupadd mysql
    useradd -g mysql mysql -M -s /sbin/nologin 
  • 我这里创建/src/目录;

    mkdir -p /src
    cd /src/ 

    如下图:

  • 下载解压;

    wget https://cdn.mysql.com//archives/mysql-5.5/mysql-5.5.54.tar.gz
    
    
    tar -zxvf mysql-5.5.54.tar.gz 

  • 进入解压目录;

    cd mysql-5.5.54 

重要:安装编译时所依赖的库和软件;

yum install cmake ncurses-devel gcc-c++ -y 

三、预编译参数:(编译参数将在后面详解也可到官方查看)

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_USER=mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \ 

如果您在cmake编译时报错需要删除编译缓存,修复错误后再次重新编译, rm -rf CMakeCache.txt

安装:(此时根据机器大概时间10-20min)

make && make install 

四、初始化数据库:

  • 进入初始化目录;

    cd /usr/local/mysql/scripts/ 
  • 指定数据目录,安装目录,用户;(注意此处为一条命令开头有个点)

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

五、测试启动下我们的mysql:

  • 进入启动脚本目录;

    cd /usr/local/mysql/support-files/ 
  • 运行启动脚本;

    ./mysql.server start 
  • 此时将会报错;(将/etc/my.cnf文件删除这里是因为系统默认了启动文件将其删除即可解决,再次运行启动脚本)

    rm - rf /etc/my.cnf 

现在我们登录mysql,它提示语法错误将安装目录的下的bin里面的mysql复制到/usr/bin/下面可解决,(也可加入全局变量)再次登录mysql已经进来了;

cd /usr/local/mysql/bin/

cp mysql /usr/bin/ 

六、设置mysql安全:

cd /src/mysql-5.5.54/scripts/

./mysql_secure_installation 

这里详细解释安全不作为命令使用:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
  SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):【这里直接回车默认数据库无密码】
Enter current password for root (enter for none): 
OK, successfully used password, moving on... 

设置root密码:

Setting the root password ensures that nobody can log 
into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y【询问是否设置root密码输入y】
New password:             【输入您想设置的新密码】          
Re-enter new password:    【重新输入新密码,注意这里输入密码的部分将不显示密码】
Reloading privilege tables..
... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]y【删除匿名用户,输入y】
... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]y

... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]y【删除测试数据库,这里输入y】
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y【询问我们现在重新加载权限表,这里输入y】
... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL! 

这时我们使用命令登录MySQL:【-p后面也可直接输密码】

mysql -uroot -p 

七、配置my.cnf到安装目录的etc下面当然这个文件可多地方配置并非只有此处;(注意此处复制的文件my-huge.cnf)

mkdir -p /usr/local/mysql/etc

cd /usr/local/mysql/support-files

cp my-huge.cnf ../etc/my.cnf 

八、设置mysql系统命令启动和开机启动如下图;

  • 复制安装目录下的启动脚本到/etc/init.d/命名为mysqld;

    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 
  • 设置开机启动;

    chmod +x /etc/init.d/mysqld
    
    
    chkconfig --add mysqld 
  • 下列命令顺序;(启动,停止,重启,状态)

    systemctl start mysqld
    systemctl stop mysqld
    systemctl restart mysqld
    systemctl status mysqld 
  • 可使用grep或netstat命令查看;

    ps -ef |grep mysqld
    
    
    netstat -ntlp 

MySQL的基本安装和设置已经完成,后续知识大家自行博学。

联系作者:http://blog.mxsina.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值