install mysql on linux

It is more convenient to use yum to install, please jump directly to "5"

1. Download mysql

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

 

 2. Unzip the installation

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) Run sudo bin/mysqld --initialize --user=mysql to report an error

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

    Solution

    The installation packages libaio and libaio-devel are missing. Execute yum install libaio to automatically install these two packages

   (2) Run sudo chown -R mysql data mysql-files to report an error

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

  (3) Run sudo bin/mysqld_safe --user=mysql & report an error

[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

    View logs

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.

    Solution

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

   error again 

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

    Solution 

#Check the running status of the database
/etc/init.d/mysql status
#hint
ERROR! MySQL is running but PID file could not be found
#print MYSQL process
ps to | grep mysql
#KILL process
kill -9 pid1 pid2 …
#Restart MYSQL
/etc/init.d/mysql start
#Check the running status of mysql
/etc/init.d/mysql status

  

3. Connect to the database

 (1) Execute the command mysql to report an error

bash: mysql: command not found

    Solution

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

    Then execute mysql, and report an error

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

    Solution

#Since mysql's default mysql.sock is in /var/lib/mysql/mysql.sock, but the linux system always goes to /tmp/mysql.sock to find it, so an error will be reported
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;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326835642&siteId=291194637