Linux下MySql的安装

1.下载

可以官网下载也可以通过wget下载
官网地址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads

cd /usr/local/
wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

2.解压并重命名文件夹

tar -xvf mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.6.41-linux-glibc2.12-x86_64 mysql

3.添加系统mysql组和mysql用户

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

4.进入mysql目录更改权限

cd mysql
chown -R mysql:mysql ./

5.安装数据库,此处可能出现错误

./scripts/mysql_install_db --user=mysql
  • 可能错误1
    FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper
    解决方法:
yum install -y perl-Data-Dumper
  • 可能错误2
    linux libaio.so.1: cannot open shared object file: No such file or directory
    解决方法:
yum install libaio*

6.修改当前目录拥有者为root用户,修改data目录拥有者为mysql

chown -R root:root ./
chown -R mysql:mysql data

MySql安装结束


7.添加mysql服务开机自启动

cp support-files/mysql.server /etc/init.d/mysql
# 赋予可执行权限
chmod +x /etc/init.d/mysql
# 添加服务
chkconfig --add mysql 
# 显示服务列表
chkconfig --list

如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则执行

chkconfig --level 345 mysql on

8.启动、关闭、重启MySql

 service mysql start
 service mysql stop
 service mysql restart

执行开启mysql

service mysql start
  • 可能错误1
    Starting MySQL.180822 21:33:57 mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists.Create writable for user 'mysql'.The server quit without updating PID file (/var/lib/mysql/i[FAILED]c2bbcfdqcpcrcz.pid).
    解决方法:
mkdir /var/log/mariadb 
touch /var/log/mariadb/mariadb.log 
chown -R mysql:mysql  /var/log/mariadb/

以上命令不行可以使用以下命令启动

sudo -u mysql service mysql start

9.本地连接数据库

由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件

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

设置密码

mysqladmin -u root password "newpwd"

连接数据库

mysql -u root -p  

输入密码报错
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决办法

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock 

10.远程连接数据库

开启远程访问权限

#先登录mysql
mysql -u root -p 
#设置所有ip可以登录mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;   
#grant all privileges on 库名.表名 to '用户名'@'IP地址' identified by '密码' with grant option;
#刷新权限 
FLUSH PRIVILEGES;

你可能感兴趣的:(Linux下MySql的安装)