美文网首页
Linux 下安装mysql

Linux 下安装mysql

作者: stamSuper | 来源:发表于2020-01-09 16:39 被阅读0次

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

  chkconfig iptables off 

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

image image image image image

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 
 
 # These are commonly set, remove the # and set as required. 
 basedir = /usr/local/mysql 
 datadir = /usr/local/mysql/data 
 port = 3306 
 # server_id = ..... 
 socket = /tmp/mysql.sock 
 character-set-server = utf8 
 skip-name-resolve 
 log-err = /usr/local/mysql/data/error.log 
 pid-file = /usr/local/mysql/data/mysql.pid 
 
 # 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
image.png

如果提示错误:
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper

就安装 autoconf库

    yum-y install autoconf

如果提示如下错误:
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory


image.png

问题:缺少libaio库文件
解决方法:

    yum install libaio* -y

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

11、添加远程访问权限

(1)、添加mysql命令

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

(2)、更改访问权限

登录mysql,执行下面命令
mysql -uroot -p 
密码为空直接回车,运行以下两条命令
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'duan' 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 'duan'exit

MySQL数据库远程连接、创建新用户、设置权限

1、新建用户

创建ssh用户,密码是ssh。localhost 就是本地连接,即127.0.0.1。

%用于远程连接,即任意ip都可以链接。

    MySQL -u root -p 
    CREATE USER 'ssh'@'localhost'  IDENTIFIED BY 'ssh'; #本地登录 
    CREATE USER 'ssh'@'%'  IDENTIFIED BY 'ssh'; #远程登录 
    quit   #先退出,在测试
    mysql -ussh -p #测试是否创建成功
image.png image.png

2、为用户授权

a.授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码';

b.登录MYSQL,这里以ROOT身份登录:

    mysql -u root -p

c.为用户创建一个数据库(test ):

    create database test DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

创建后用show databases;查看数据库:


image.png

d.授权ssh用户拥有test数据库的所有权限:

    grant all privileges on `test`.* to 'ssh'@'localhost' identified by 'ssh'; 
    grant all privileges on `test`.* to 'ssh'@'%' identified by 'ssh';


    flush privileges; #刷新系统权限表

注意:MySQL中字符要求很严格,必须用英文小写字符!


image.png

3、删除用户

mysql -u root -p 
Delete FROM mysql.user Where User=”test” and Host=”localhost”; 
flush privileges; 
drop database testDB;
image.png
 删除账户及权限:

drop user 用户名@’%’; 
drop user 用户名@ localhost;

注意:有可能出现的问题:

使用以下命令行删除账户:

    delete from user where user='账户名';

出现:

    ERROR 1046 (3D000): No database selected 错误:没有选中数据库。

因为是直接使用 SQL 语句的方式来删除账户,所以必须先选择 mysql 自身的数据库:

    use mysql;

好了,现在用ssh账户登陆,开始建表!

create table test(
   test_id INT NOT NULL AUTO_INCREMENT,
   test_date DATE,
   PRIMARY KEY ( test_id )
);

好了,建表之后可以查看表:


image.png

这个时候需要用命令查看数据库所用端口,这个很重要哦,远程连接数据库时候需要的!而且前面咱们授权ssh账户远程登录时,一定要有成功授权提示哦!

    show variables  like 'port';
image.png image.png

接下来,就可以打开 navicat 进行连接测试了

参考文档:浩coding

mysql设置root密码

打开终端,登录centos服务器。

刚初始化完毕mysql,这个时候只要在命令行里面输入mysql,是可以直接登陆的(无需输入密码)。

image.png

可以查看root用户下面的数据库

image.png

为mysql设置初始化密码的命令为:mysql -uroot -p 'xxxxxx'

    mysqladmin -u root password 'xxxxxx'
image.png

设置完毕密码之后,再次使用mysql命令登录数据库,登录请求就会被拒绝。必须使用修改后的密码登录

image.png

相关文章

网友评论

      本文标题:Linux 下安装mysql

      本文链接:https://www.haomeiwen.com/subject/akoyactx.html