在Linux安装mysql

使用yum安装更为方便,请直接跳转到“五”

一、下载mysql

wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz

 

 二、解压安装

sudo groupadd mysql
sudo useradd -r -g mysql -s /bin/false mysql
sudo cd /usr/local
sudo tar zxvf /path/to/mysql-VERSION-OS.tar.gz
sudo ln -s full-path-to-mysql-VERSION-OS mysql
sudo cd mysql
sudo mkdir mysql-files
sudo chmod 770 mysql-files
sudo chown -R mysql .
sudo chgrp -R mysql .
sudo bin/mysql_install_db --user=mysql    # Before MySQL 5.7.6
sudo bin/mysqld --initialize --user=mysql # MySQL 5.7.6 and up
sudo bin/mysql_ssl_rsa_setup              # MySQL 5.7.6 and up
sudo chown -R root .
sudo chown -R mysql data mysql-files
sudo bin/mysqld_safe --user=mysql &
# Next command is optional
sudo cp support-files/mysql.server /etc/init.d/mysql.server

   (1)运行sudo bin/mysqld --initialize --user=mysql报错

bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

    解决办法

    缺少安装包libaio和libaio-devel,执行yum install libaio自动安装这两个包

   (2)运行sudo chown -R mysql data mysql-files报错

            “chown: cannot access `data': No such file or directory”

  (3)运行sudo bin/mysqld_safe --user=mysql &报错

[1] 5509
[huanghaifeng@vm20251 mysql]$ Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
grep: write error: Broken pipe
151229 19:17:05 mysqld_safe Logging to '/var/log/mysqld.log'.
151229 19:17:05 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
151229 19:18:45 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

    查看日志

2015-12-29T11:17:42.524999Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2015-12-29T11:17:42.525047Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.

    解决办法

ps -ef | grep mysql
kill -9 number
sudo /etc/init.d/mysql restart

   又报错 

ERROR! MySQL server process #5454 is not running!
Starting MySQL. SUCCESS!

    解决办法 

#查看下数据库运行状态
/etc/init.d/mysql status
#提示
ERROR! MySQL is running but PID file could not be found
#打印MYSQL进程
ps aux | grep mysql
#KILL进程
kill -9 pid1 pid2 …
#重新启动MYSQL
/etc/init.d/mysql start
#检查mysql运行状态
/etc/init.d/mysql status

  

三、连接数据库

 (1)执行命令mysql报错

 bash: mysql: command not found

    解决办法

sudo ln -s /usr/local/mysql/bin/mysql /usr/bin

    然后执行mysql,又报错

[huanghaifeng@vm20251 druid-0.8.2]$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    解决办法

#由于mysql 默认的mysql.sock 是在/var/lib/mysql/mysql.sock,但linux系统总是去/tmp/mysql.sock查找,所以会报错
find / -name mysql.sock
#输出/var/lib/mysql/mysql.sock
 方法一:
#直接指定mysql通道
 mysql --socket=/var/lib/mysql/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 2 to server version: 5.0.22
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
mysql>
方法二: 
# 创建符号连接,为mysql.sock增加软连接(相当于windows中的快捷方式)。
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

    执行还是报错

#方法一:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)
#方法二:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (13)

    查询mysql状态

/etc/rc.d/init.d/mysql.server status

    报错信息

ERROR! MySQL is running but PID file could not be found

 查很多原因,主要解决办法是kill进程,然后重启服务,实在是不能忍了,

我们改用yum安装吧。

 

四、删除已安装mysql

#查看安装的mysql
yum list installed | grep mysql
#卸载mysql
sudo yum -y remove mysql-libs.x86_64
sudo yum -y remove mysql.x86_64
#检查是否删干净了
yum list installed | grep mysql
whereis mysql

 

五、yum安装mysql

#查询yum中是否有mysql
yum list | grep mysql
#安装
sudo yum -y install mysql-server mysql mysql-devel
#检查安装版本
rpm -qi mysql-server

    启动,又在开始报错

[huanghaifeng@vm20251 local]$ sudo /etc/init.d/mysqld start
MySQL Daemon failed to start.
Starting mysqld:                                           [FAILED]
#查看日志
151230 15:30:31 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
151230 15:30:32 [Warning] '--default-character-set' is deprecated and will be removed in a future release. Please use '--character-set-server' instead.
/usr/libexec/mysqld: Can't find file: './mysql/plugin.frm' (errno: 13)
151230 15:30:32 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
151230 15:30:32  InnoDB: Initializing buffer pool, size = 8.0M
151230 15:30:32  InnoDB: Completed initialization of buffer pool
151230 15:30:32  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name ./ibdata1
InnoDB: File operation call: 'open'.
InnoDB: Cannot continue operation.
151230 15:30:32 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

    解决办法

vi /etc/my.cnf
#将这一行datadir=/var/lib/mysql修改为
datadir=/data/db
#然后用安全模式启动mysql
/usr/bin/mysqld_safe & 

 

六、mysql基本命令

#修改root用户名
mysqladmin -u root -p password "test123"
#登录本机mysql
mysql -u用户名 -p密码
#登录远程mysql
mysql -h ip  -u用户名 -p密码
#退出
quit
#创建用户grant select on 数据库.* to 用户名@登录主机 identified by '密码'
grant select,insert,update,delete on *.* to username@localhost identified by 'password';
#删除用户
delete from  where user='用户名' and host='localhost';
#列出数据库
show databases;
#删除数据库
drop database test;
#创建数据库
create database test;
#连接数据库
use test;
#查看当前使用的数据库
select database();
#当前数据库包含的表信息
show tables;
#创建表
create table MyClass(id int(4) not null primary key auto_increment,name char(20) not null,sex int(4) not null default '0',degree double(16,2));
#获取表结构
describe MyClass
desc MyClass;
show columns from MyClass;
#删除表
drop table MyClass;

你可能感兴趣的:(linux,mysql,yum,rpm)