linux多种方式安装mysql教程

以下是linux安装mysql的几种教程,供大家分享

安装方式1:源码安装

准备工作:

1.下载安装包

下载源码http://dev.mysql.com/downloads/mysql/

文件名:mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz

2.查看当前centos版本,内核和位数

命令:

cat /etc/redhat-release

cat /proc/version

uname -a

系统是:CentOS release 6.5 (Final),64位操作系统

开始安装

mysql压缩包上传到/usr/local 目录下面。安装目录选择/usr/local/mysql(mysql文件夹自行创建)

1.解压tar

tar -xzvf mysql-5.6.25-linux-glibc2.5-x86_64.tar.gzmysql目录

2.添加用户与组

groupadd mysql

useradd -r -g mysql mysql

chown -R mysql:mysql mysql  (后一个"mysql"为安装目录名称)

3.安装数据库

su mysql

cd /usr/local/mysql/scripts

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

Exit

(这里可能会报错:scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory,需要安装perl以及perl-devel.执行命令:yumy install perl perl-devel)

4.配置文件

cd /usr/local/mysql/support-files

cp my-default.cnf /etc/my.cnf

cp mysql.server /etc/init.d/mysql

vim /etc/init.d/mysql    #mysql的安装目录是/usr/local/mysql,则可省略此步

修改文件中的两个变更值

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

 

5.配置环境变量

vim /etc/profile

export MYSQL_HOME="/usr/local/mysql"

export PATH="$PATH:$MYSQL_HOME/bin"

保存退出

. /etc/profile

6.添加自启动服务

chkconfig --add mysql

chkconfig mysql on

7.启动mysql

service mysql start/etc/init.d/mysql start

8.登录mysql及改密码与配置远程访问

mysqladmin -u root password '123456'     #修改root用户密码

如出现问题 输入cat /root/.mysql_secret查看mysql初始密码登录,或者查看博客:http://blog.csdn.net/zl17/article/details/8589454

mysql -u root -p     #登录mysql,需要输入密码

更改密码

mysql>use mysql #选择要操作的数据库

mysql>SELECT User, Password ,Host From user;

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;   #允许root用户远程访问

mysql>flush privileges; #刷新权限

mysql>exit

特别注意:

在选择完数据库,输入操作数据库语句时一定要带”;”,否则操作不成功,如果仅仅出现”->”而不是”mysql>”这样的字样,应该ctrl+c退出,否则操作不成功


本方式安装参考:http://blog.csdn.net/cuker919/article/details/46481427

安装方式2:rpm安装

Rpm安装mysql

检查卸载以前版本

检查MySQL及相关RPM包,是否安装,如果有安装,则移除

rpm -qa | grep -i mysql

yum -y remove 包名 或者rpm -e  包名

rm -fr /usr/lib/mysql  //删除老版本mysql的开发头文件和库

rm -fr /usr/include/mysql

注意:卸载后/var/lib/mysql中的数据及/etc/my.cnf不会删除,如果确定没用后就手工删除

rm -f /etc/my.cnf

rm -fr /var/lib/mysql

下载rpm

 

下载Linux对应的RPM包/usr/local/rpm,如下

MySQL-server-5.6.33-1.linux_glibc2.5.i386.rpm

MySQL-client-5.6.33-1.linux_glibc2.5.i386.rpm

MySQL-devel-5.6.33-1.linux_glibc2.5.i386.rpm

安装MySQL

cd /usr/local/rpm

rpm -ivh MySQL-server-5.6.33-1.linux_glibc2.5.i386.rpm

rpm -ivh MySQL-client-5.6.33-1.linux_glibc2.5.i386.rpm

rpm -ivh MySQL-devel-5.6.33-1.linux_glibc2.5.i386.rpm

#安装时提示缺少响应的插件,查看资料http://www.th7.cn/db/mysql/201504/102776.shtml 提示缺少什么插件,进行yum install 插件名 进行安装

如果还是部分插件无法安装:http://blog.csdn.net/qianlong4526888/article/details/40108591



初始化MySQL及设置密码

cp /usr/share/mysql/my-default.cnf /etc/my.cnf #修改配置文件位置

/usr/bin/mysql_install_db

service mysql start

#可能出现mysql Starting MySQL..The server quit without updating PID file解决:http://www.111cn.net/database/mysql/47258.htm


cat /root/.mysql_secret  #查看root账号密码


mysql -uroot -p初始密码(-p与密码中间无空格)

mysql>SET PASSWORD = PASSWORD('123456');#设置新密码

mysql> use mysql;

mysql> select host,user,password from user;

mysql> update user set password=password('123456') where user='root';

mysql> update user set host='%' where user='root' and host='localhost';#设置远程登录

mysql> flush privileges;

mysql> exit

开机自启

chkconfig mysql on

chkconfig --list | grep mysql

MySQL的默认安装位置

/var/lib/mysql/               #数据库目录

/usr/share/mysql              #配置文件目录

/usr/bin                     #相关命令目录

/etc/init.d/mysql              #启动脚本可以用/etc/init.d/mysql  start进行启动mysql

修改字符集和数据存储路径

配置/etc/my.cnf文件,修改数据存放路径、mysql.sock路径以及默认编码utf-8.

[client]  
password        = 123456  
port            = 3306  
default-character-set=utf8  
[mysqld]  
port            = 3306  
character_set_server=utf8  
character_set_client=utf8  
collation-server=utf8_general_ci  
#(注意linux下mysql安装完后是默认:表名区分大小写,列名不区分大小写; 0:区分大小写,1:不区分大小写)  
lower_case_table_names=1  
#(设置最大连接数,默认为 151,MySQL服务器允许的最大连接数16384; )  
max_connections=1000  
[mysql]  
default-character-set = utf8 

查看字符集

 

show variables like '%collation%';

show variables like '%char%';

本方式安装参考:http://blog.csdn.net/liumm0000/article/details/18841197/

安装方式3:源码安装(脚本方式)

安装流程:

1.mysql官网下载二进制版本的mysql5.7.15安装包(mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz)/root目录

wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz

2.执行下文所述的mysql_auto_install.sh脚本

3.chmod 777 mysql_auto_install.sh

sh mysql_auto_install.sh

4.输入您设置的密码登录数据库

 

二、

ln -s /usr/local/mysql/bin/mysql /usr/bin   创建软连接

 mysql -uroot -p

三、5.3、设置允许root从任何IP地址登陆

默认情况下,mysql只允许locathost本地登陆,用Navicat是无法远程登陆的。

登录mysql:输入如下命令

grant all privileges on *.* to 'root'@'%' identified by '123456';   #123456是登录密码

flush privileges;

 

四、

脚本如下:执行即可

cat mysql_auto_install.sh
###### 二进制自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可###############
######数据库目录/usr/local/mysql############
######数据目录/data/mysql############
######慢日志目录/data/slowlog############
######端口号默认3306其余参数按需自行修改############
 
##################
#author:935955868@qq.com#
##################
#!/bin/bash
 
 
# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi
 
clear
echo "========================================================================="
echo "A tool to auto-compile & install MySQL 5.7.15 on Redhat/CentOS Linux "
echo "========================================================================="
cur_dir=$(pwd)
 
#set mysql root password
    echo "==========================="
 
    mysqlrootpwd="MANAGER"
    echo -e "Please input the root password of mysql:"
    read -p "(Default password: MANAGER):" mysqlrootpwd
    if [ "$mysqlrootpwd" = "" ]; then
        mysqlrootpwd="MANAGER"
    fi
    echo "==========================="
    echo "MySQL root password:$mysqlrootpwd"
    echo "==========================="
 
#which MySQL Version do you want to install?
echo "==========================="
 
 
    isinstallmysql57="n"
    echo "Install MySQL 5.7.15,Please input y"
    read -p "(Please input y , n):" isinstallmysql57
 
 
    case "$isinstallmysql57" in
    y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
    echo "You will install MySQL 5.7.15"
 
    isinstallmysql57="y"
    ;;
    *)
    echo "INPUT error,You will exit install MySQL 5.7.15"
 
    isinstallmysql57="n"
    exit
    esac
 
    get_char()
    {
    SAVEDSTTY=`stty -g`
    stty -echo
    stty cbreak
    #dd if=/dev/tty bs=1 count=1 2> /dev/null
    stty -raw
    stty echo
    stty $SAVEDSTTY
    }
    echo ""
    echo "Press any key to start...or Press Ctrl+c to cancel"
    char=`get_char`
 
# Initialize  the installation related content.
function InitInstall()
{
    cat /etc/issue
    uname -a
    MemTotal=`free -m | grep Mem | awk '{print  $2}'`  
    echo -e "\n Memory is: ${MemTotal} MB "
    #Set timezone
    #rm -rf /etc/localtime
    #ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 
 
    #Delete Old Mysql program
    rpm -qa|grep mysql
    rpm -e mysql
 
 
 
 
 
 
    #Disable SeLinux
    if [ -s /etc/selinux/config ]; then
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    fi
    setenforce 0
 
 
}
 
 
#Installation of depend on and optimization options.
function InstallDependsAndOpt()
{
cd $cur_dir
 
cat >>/etc/security/limits.conf<<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
 
echo "fs.file-max=65535" >> /etc/sysctl.conf
}
 
#Install MySQL
function InstallMySQL57()
{
echo "============================Install MySQL 5.7.15=================================="
cd $cur_dir
 
#Backup old my.cnf
#rm -f /etc/my.cnf
if [ -s /etc/my.cnf ]; then
    mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bak
fi
 
echo "============================MySQL 5.7.15 installing…………========================="
#mysql directory configuration
tar xvf /root/mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
mv /root/mysql-5.7.15-linux-glibc2.5-x86_64 /usr/local/mysql
groupadd mysql -g 512
useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysql
mkdir -p /data/mysql
mkdir -p /data/slowlog
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/slowlog
 
 
#edit /etc/my.cnf
SERVERID=`ifconfig eth0 | grep "inet addr" | awk '{ print $2}'| awk -F. '{ print $3$4}'`
cat >>/etc/my.cnf<<EOF
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8
 
[mysql]
no-auto-rehash
default-character-set=utf8
 
[mysqld]
port=3306
character-set-server=utf8
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/data/mysql
explicit_defaults_for_timestamp=true
lower_case_table_names=1
back_log=103
max_connections=3000
max_connect_errors=100000
table_open_cache=512
external-locking=FALSE
max_allowed_packet=32M
sort_buffer_size=2M
join_buffer_size=2M
thread_cache_size=51
query_cache_size=32M
#query_cache_limit=4M
transaction_isolation=REPEATABLE-READ
tmp_table_size=96M
max_heap_table_size=96M
 
###***slowqueryparameters
long_query_time=1
slow_query_log = 1
slow_query_log_file=/data/slowlog/slow.log
 
###***binlogparameters
log-bin=mysql-bin
binlog_cache_size=4M
max_binlog_cache_size=8M
max_binlog_size=1024M
binlog_format=MIXED
expire_logs_days=7
 
###***relay-logparameters
#relay-log=/data/3307/relay-bin
#relay-log-info-file=/data/3307/relay-log.info
#master-info-repository=table
#relay-log-info-repository=table
#relay-log-recovery=1
 
#***MyISAMparameters
key_buffer_size=16M
read_buffer_size=1M
read_rnd_buffer_size=16M
bulk_insert_buffer_size=1M
 
#skip-name-resolve
 
###***master-slavereplicationparameters
server-id=$SERVERID
#slave-skip-errors=all
 
#***Innodbstorageengineparameters
innodb_buffer_pool_size=512M
innodb_data_file_path=ibdata1:10M:autoextend
#innodb_file_io_threads=8
innodb_thread_concurrency=16
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=16M
innodb_log_file_size=512M
innodb_log_files_in_group=2
innodb_max_dirty_pages_pct=75
innodb_buffer_pool_dump_pct=75
innodb_lock_wait_timeout=50
innodb_file_per_table=on
 
[mysqldump]
quick
max_allowed_packet=32M
 
[myisamchk]
key_buffer=16M
sort_buffer_size=16M
read_buffer=8M
write_buffer=8M
 
[mysqld_safe]
open-files-limit=8192
log-error=/data/mysql/error.log
pid-file=/data/mysql/mysqld.pid
 
EOF
 
 
 
 
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql --initialize-insecure
 
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
 
cat >> /etc/ld.so.conf.d/mysql-x86_64.conf<<EOF
/usr/local/mysql/lib
EOF
ldconfig
 
if [ -d "/proc/vz" ];then
ulimit -s unlimited
fi
 
/etc/init.d/mysqld start
 
 
cat >> /etc/profile <<EOF
export PATH=$PATH:/usr/local/mysql/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib
EOF
 
/usr/local/mysql/bin/mysqladmin -u root password $mysqlrootpwd
 
cat > /tmp/mysql_sec_script<<EOF
use mysql;
delete from mysql.user where user!='root' or host!='localhost';
grant all privileges on *.* to 'sys_admin'@'%' identified by 'MANAGER';
flush privileges;
EOF
 
/usr/local/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script
 
#rm -f /tmp/mysql_sec_script
 
 
#/etc/init.d/mysqld restart
 
 
 
 
echo "============================MySQL 5.7.15 install completed========================="
}
 
 
 
function CheckInstall()
{
echo "===================================== Check install ==================================="
clear
ismysql=""
echo "Checking..."
 
if [ -s /usr/local/mysql/bin/mysql ] && [ -s /usr/local/mysql/bin/mysqld_safe ] && [ -s /etc/my.cnf ]; then
  echo "MySQL: OK"
  ismysql="ok"
  else
  echo "Error: /usr/local/mysql not found!!!MySQL install failed."
fi
 
if [ "$ismysql" = "ok" ]; then
echo "Install MySQL 5.7.15 completed! enjoy it."
echo "========================================================================="
netstat -ntl
else
echo "Sorry,Failed to install MySQL!"
echo "You can tail /root/mysql-install.log from your server."
fi
}
 
#The installation log
InitInstall 2>&1 | tee /root/mysql-install.log
InstallDependsAndOpt 2>&1 | tee -a /root/mysql-install.log
InstallMySQL57 > /dev/null
CheckInstall 2>&1 | tee -a /root/mysql-install.log

Mysql其他

1、查询当前安装的mysql版本

mysql -V

进入mysql、查看mysql版本

status;

2、Mysql启动、停止、重启常用命令

方法一:使用 service 启动(有些版本是mysqld)

service mysql start   #开启

service mysql  restart  #重启

service mysql stop  #关闭

方法二:脚本方式: 

 /etc/init.d/mysql start   #尝试启动服务  

/etc/init.d/mysql restart  #重启服务

/etc/init.d/mysql stop  #关闭服务

 

3.忘记本地密码

 

#1.停止mysql数据库

/etc/init.d/mysql stop

#2.执行如下命令

mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

#3.使用root登录mysql数据库

mysql -u root mysql

#4.更新root密码

mysql> UPDATE user SET Password=PASSWORD('123456') where USER='root'

#5.刷新权限

mysql> FLUSH PRIVILEGES

#6.退出mysql

mysql> quit

#7.重启mysql

/etc/init.d/mysql restart

#8.使用root用户重新登录mysql

mysql -uroot -p

Enter password: <输入新设的密码newpassword>

 

4.赋予权限

 

(1)、创建用户:

 

create user 'myuser'@'localhost' IDENTIFIED by '123456';

 

flush privileges;

 

(2)、创建数据库:

 

create database mydb;

 

flush privileges;

 

(3)、给用户授权数据库相关权限

 

grant all privileges on mydb.* to myuser@localhost identified by '123456';

 

flush privileges;

 

我用到的权限命令,简单说明,mydb为数据库实例名,myuser为用户名,后面对某个ip作对应的权限控制。

grant 为赋予权限,revoke为撤销权限,赋予权限完之后要用flush privileges; 来刷新生效。

 

1grant all on mydb.* to 'myuser'@'192.168.1.6'identified by '123456';

2grant select on mydb.* to 'myuser'@'192.168.1.%' identified by '123456';

3revoke all on mydb.* from 'myuser'@'192.168.1.6' ;

 

 

 

grant all on mydb.* to 'myuser'@'localhost' identified by '123456';

 

授权之后,启动应用。今天恢复数据库之后发现一个问题,系统一直报错:

 

 

Java.sql.SQLException: Access denied for user 'myuser'@'127.0.0.1' (using password: YES)

 

还要配置一个127.0.0.1的。

 

grant all on mydb.* to 'myuser'@'127.0.0.1' identified by '123456';

 

查看用户权限

show grants for 你的用户

比如:

show grants for root@localhost;

 

show grants for  'myuser'@'192.168.1.1';

 

 

mysql> use mysql;

Database changed

mysql> select user,host from user;

 

具体参考:http://www.cnblogs.com/llsun/archive/2013/08/06/3240963.html

 







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨港飞燕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值