Linux下mysql安装配置

时间:2022-05-26 10:25:47

mysql安装配置:

1.下载
进入要下载的版本的目录,选择.tar.gz文件下载
下载链接: https://www.mysql.com/downloads/

2.安装
使用tar解压要安装的目录即可,以5.6.版本为例
[root@localhost mysql]# tar -zxvf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz
[root@localhost mysql]# mv mysql-5.6.33-linux-glibc2.5-x86_64/ mysql

3.添加用户和用户组
#添加用户组
[root@localhost mysql]# groupadd mysql
#添加用户mysql 到用户组mysql
[root@localhost mysql]# useradd -g mysql mysql

4.配置mysql
创建数据目录
[root@localhost mysql]# mkdir data
数据库数据默认目录datadir=/var/lib/mysql,可通过vim /etc/my.cnf 查看
设置权限:
[root@localhost mysql]# chown -R mysql:mysql data
[root@localhost mysql]# cd data
[root@localhost data]# pwd
/home/svr/application/mysql/data
执行安装脚本:
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/home/svr/application/mysql/data
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld
[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf
 
#修改启动脚本
[root@localhost mysql]# vi /etc/init.d/mysqld
 
#修改项(修改成自己服务上的目录):
basedir=/home/svr/application/mysql/mysqldatadir=/home/svr/application/mysql/data
#启动服务
[root@localhost mysql]# service mysqld start
相关命令:
#启动mysql
service mysqld start
#关闭mysql
service mysqld stop
#查看运行状态
service mysqld status
 
#加入环境变量,编辑 /etc/profile,这样可以在任何地方用mysql命令了
[root@localhost mysql]# vi /etc/profile
export PATH=$PATH:/home/svr/application/mysql/mysql
#测试连接
[root@localhost mysql]#  ./mysql/bin/mysql -uroot

Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.33 MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               |+--------------------+4 rows in set (0.31 sec)

5、建库与删库:
create database 库名;
drop database 库名;

建表:
use 库名;
create table 表名(字段列表);
drop table 表名;