Linux下安装mysql

1、首先关闭linux的防火墙,执行命令

chkconfig iptables off

2、从mysql官网上下载自己适合的mysql版本https://dev.mysql.com/downloads/mysql/5.6.html#downloads,进入mysql官网,依次点击

 

 

 

 

 

3、下载后的mysql文件

mysql-5.6.40-linux-glibc2.12-i686.tar.gz

 将下载好的mysql压缩文件放置在linux的/usr/local文件夹下,解压该压缩文件

 tar -zxvf mysql-5.6.40-linux-glibc2.12-i686.tar.gz

将解压后的文件重命名为mysql

mv mysql-5.6.40-linux-glibc2.12-i686 mysql

4、创建mysql用户组及用户

groupadd mysql
useradd -r -g mysql mysql

5、进入到mysql目录,执行添加MySQL配置的操作

cp support-files/my-medium.cnf /etc/my.cnf
或:
cp support-files/my-default.cnf /etc/my.cnf
是否覆盖?按y 回车

6、编辑/etc/my.cnf文件;

vi /etc/my.cnf

在my.cnf文件中添加或者修改相关配置,更改完成后保存退出

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
log_bin = /usr/local/mysql/data/log

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 1
socket = /tmp/mysql.sock
pid_file = /usr/local/mysql/data/mysql.pid
tmpdir = /tmp
log_err = /usr/local/mysql/data/log/error.log
#lower_case_table_names 大小写敏感设置的属性 
#lower_case_table_names=1:表名存储在磁盘是小写的,但是比较的时候是不区分大小写
#lower_case_table_names=0:表名存储为给定的大小和比较是区分大小写的
#lower_case_table_names=2:表名存储为给定的大小写但是比较的时候是小写的
lower_case_table_names = 1
character-set-server = utf8
skip-name-resolve
#bind_address = 127.0.0.1

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

7、在mysql当前目录下设定目录的访问权限(注意后面的小点,表示当前目录)

chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data (别忘记设置访问权限)

 

8、初始化数据(在mysql/bin或者mysql/scripts下有个 mysql_install_db 可执行文件初始化数据库),进入mysql/bin或者mysql/scripts目录下,执行下面命令

./mysql_install_db --verbose --user=root --defaults-file=/etc/my.cnf --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --pid-file=/usr/local/mysql/data/mysql.pid --tmpdir=/tmp

 

9、启动mysql,进入/usr/local/mysql/bin目录,执行下面命令

./mysqld_safe --defaults-file=/etc/my.cnf --socket=/tmp/mysql.sock --user=root &
注意,如果光标停留在屏幕上,表示启动成功,需要我们先关闭shell终端,再开启一个新的shell终端,不要执行退出操作。如果出现 mysql ended这样的语句,表示Mysql没有正常启动,你可以到log中查找问题. 

10、设置开机启动,新开启shell中断后,进入mysql目录,执行下面命令

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
chmod 700 /etc/init.d/mysql
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
chown mysql:mysql -R /usr/local/mysql/

重启linux

reboot

查看mysql状态

service mysqld status

如果mysql未启动则执行命令:service mysql start

执行命令:ps -ef|grep mysql 看到mysql服务是否启动成功

11、添加远程访问权限

  (1)、添加mysql命令

ln  -s /usr/local/mysql/bin/mysql  /usr/bin  (mysql的安装路径)

  (2)、更改访问权限

登录mysql,执行下面命令
mysql -uroot -p 
密码为空直接回车,运行以下两条命令(使用root/123456从任何主机连接到mysql服务器)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' with grant option;
Flush privileges;

退出mysql

exit

重启linux,就完成了

reboot

注:本机访问mysql,root账户默认是没有密码的,端口号默认3306,如果需要修改root账户密码,在/usr/local/mysql/bin目录下,执行下面命令

./mysqladmin -h 127.0.0.1 -P3306 -uroot password '123456'exit

安装完后的使用

安装完以后,大家可以输入mysql -uroot -p,然后enter password来进入mysql。

进去后可以使用mysql的各种语法,首先可以查看database:show databases。

 

Linux下mysql的卸载:

1、查找以前是否装有mysql

命令:rpm -qa|grep -i mysql

可以看到mysql的两个包:

mysql-4.1.12-3.RHEL4.1

mysqlclient10-3.23.58-4.RHEL4.1

2、删除mysql

删除命令:rpm -e --nodeps 包名

( rpm -ev mysql-4.1.12-3.RHEL4.1 )

3、删除老版本mysql的开发头文件和库

检查各个mysql文件夹是否删除干净

find / -name mysql

结果如下:

/var/lib/mysql

/usr/local/mysql

/usr/lib/mysql

/usr/include/mysql

命令:

rm -fr /usr/lib/mysql

rm -fr /usr/include/mysql

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

rm -f /etc/my.cnf

rm -fr /var/lib/mysql

 4.删除mysql用户及用户组

 userdel mysql

groupdel mysql

 

Linux下创建和删除软链接:

1.先建立一个软连接

 1 [root@rekfan.com test]# ls -il
 2 总计  0
 3 1491138 -rw-r–r– 1 root root 48 07-14 14:17 file1
 4 1491139 -rw-r–r– 2  root root 0 07-14 14:17 file2
 5 1491139 -rw-r–r– 2 root root 0 07-14 14:17  file2hand
 6 #建立file1和file1soft软连接
 7 [root@rekfan.com test]# ln -s file1  file1soft
 8 [root@rekfan.com test]# ls -il
 9 总计 0
10 1491138 -rw-r–r– 1 root  root 48 07-14 14:17 file1
11 1491140 lrwxrwxrwx 1 root root 5 07-14 14:24  file1soft -> file1
12 1491139 -rw-r–r– 2 root root 0 07-14 14:17  file2
13 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2hand

其中,ln -s file1 filesoft 中的file1就是源文件,file1soft就是目标链接文件名,其作用是当进入filesoft目录,实际上是链接进入了file1目录

2.删除上面建立的软连接

 1 [root@rekfan.com test]# ls -il
 2 总计  0
 3 1491138 -rw-r–r– 1 root root 0 07-14 14:17 file1
 4 1491140 lrwxrwxrwx 1  root root 5 07-14 14:24 file1soft -> file1
 5 1491139 -rw-r–r– 2 root root 0  07-14 14:17 file2
 6 1491139 -rw-r–r– 2 root root 0 07-14 14:17  file2hand
 7 #删除软连接
 8 [root@rekfan.com test]# rm -rf file1soft
 9 [root@rekfan.com test]#  ls -il
10 总计 0
11 1491138 -rw-r–r– 1 root root 0 07-14 14:17 file1
12 1491139  -rw-r–r– 2 root root 0 07-14 14:17 file2
13 1491139 -rw-r–r– 2 root root 0 07-14  14:17 file2hand

 

启动mysql时显示:/tmp/mysql.sock 不存在的解决方法

1 [root@localhost mysql]# bin/mysqladmin -u root password root
2 bin/mysqladmin: connect to server at 'localhost' failed
3 error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
4 Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
5 [root@localhost mysql]# bin/mysql -u root -p
6 Enter password:
7 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
8 分析:是/tmp/mysql.sock 不存在

由于搜索的mysql.sock路径是在/tmp下,而mysql安装的mysql.sock在/var/lib/mysql下,所以选择建立符号(软)连接:

1 # ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
2 # bin/mysql -u root
3 Welcome to the MySQL monitor. Commands end with ; or g.
4 Your MySQL connection id is 1
5 Server version: 5.0.45 MySQL Community Server (GPL)
6 Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
7 mysql>

安装mysql报错:FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper

解决方法 :安装autoconf库

命令:yum-y install autoconf   //此包安装时会安装Data:Dumper模块

 

启动mysql报错:Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/mysql/cnsz22VLK15556.pid).

1、可能是/usr/local/mysql/data/mysql.pid文件没有写的权限
解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data” “chmod -R 755 /usr/local/mysql/data”  然后重新启动mysqld!

2、可能进程里已经存在mysql进程
解决方法:用命令“ps -ef|grep mysqld”查看是否有mysqld进程,如果有使用“kill -9  进程号”杀死,然后重新启动mysqld!

3、可能是第二次在机器上安装mysql,有残余数据影响了服务的启动。
解决方法:去mysql的数据目录/data看看,如果存在mysql-bin.index,就赶快把它删除掉吧,它就是罪魁祸首了。

4、mysql在启动时没有指定配置文件时会使用/etc/my.cnf配置文件,请打开这个文件查看在[mysqld]节下有没有指定数据目录(datadir)。
解决方法:请在[mysqld]下设置这一行:datadir = /usr/local/mysql/data

5、skip-federated字段问题
解决方法:检查一下/etc/my.cnf文件中有没有没被注释掉的skip-federated字段,如果有就立即注释掉吧。

6、错误日志目录不存在。(本人是这个方法解决的)
解决方法:使用“chown” “chmod”命令赋予mysql所有者及权限

7、selinux惹的祸,如果是centos系统,默认会开启selinux
解决方法:关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器试试。

转自:https://www.cnblogs.com/fnlingnzb-learner/p/5830622.html

希望此文能够帮助到您!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值