mysql安装与使用教程(超详细MySQL安装及基本使用教程)

安装mysql(windows)

  • 下载安装资源mysql80和VC_redist_x64
  • 双击mysql80安装(可选择Developer Default或者Custom)

mysql安装与使用教程(超详细MySQL安装及基本使用教程)

  • 如果选择Custom安装,则可以选择想要安装的产品,下图中我们选择了【server(必须),workbench(必须),shell(可选)),Doc(可选)和Exmaple(可选)以及三个连接器用于从代码访问mysql数据库(ODBC,python,java,c++等,可选】。注意:没列出的组件均可随喜好安装,安装是选择版本最好统一。[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
  • 检查依赖,通过安装界面提示安装所需库,我这边安装显示缺少库VC_redist_x64,下载安装即可。

mysql安装与使用教程(超详细MySQL安装及基本使用教程)

  • 下载安装组件,请保持网络连接。
  • 选择服务器配置类型:默认设置。
  • 认证方法:随意。
  • 设置root用户密码和管理员用户名,管理员用户密码。此处建议使用<name: lyp1234, password: 1234>这种

mysql安装与使用教程(超详细MySQL安装及基本使用教程)

  • 完成安装。注意安装后可以使用安装器增加额外的组件,单个组件可以在[控制面板->程序]中手动删除。

安装mysql(linux)

Ubuntu:

sudo apt-get install mysql-server
1

详情见 https://blog.csdn.net/weixx3/article/details/80782479

CentOS:

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update
yum install mysql-server
1234

详情见 https://www.runoob.com/mysql/mysql-install.html

其它linux:自行查找资料

安装后密码为空,可使用命令sudo mysqladmin -u root password";修改mysql root用户密码。

安装mysql(MacOS)

本人买不起MAC,MAC安装mysql教程 ,大致和windows安装流程相似。

启动mysql服务

Windows:

  • 将mysql可执行命令添加到环境变量。mysql默认安装位置默认为C:Program FilesMySQLMySQL Server 8.0bin,将其添加到环境变量中去即可在cmdline直接调用mysql。
  • 添加环境变量后,打开一个具有管理员权限的cmd,输入命令net start mysql80启动mysql服务。

linux:

  • 在命令行使用service mysql start/stop打开或关闭mysql服务

MacOS:

  • 在命令行使用sudo /usr/local/mysql/support-files/mysql.server start/stop打开或关闭mysql服务

从命令行连接mysql

  • 启动mysql服务后,可以在一般权限的命令行中使用命令mysql -u <your user name> -p并输入密码启动mysql命令行。
  • 以root用户在Linux系统登录需要sudo权限。

mysql安装与使用教程(超详细MySQL安装及基本使用教程)

  • 使用mysql命令行:创建数据库:create database test启动test数据库: use test在数据库下执行基本的sql命令。这里是基本的sql命令

使用workbench连接mysql(windows/MacOS only)

  • 登录root用户,点击local instance MySQL80即可建立数据库连接
  • 登录我的用户:点击上图中+号新建一个连接。

mysql安装与使用教程(超详细MySQL安装及基本使用教程)

  • 使用sql编辑器测试mysql,点击闪电按钮可执行选中语句create database test; use test; #drop table tab1; create table tab1 ( ID int not null, name varchar(10) ); insert into tab1 (ID, name) values (1, 'aaa'); insert into tab1 (ID, name) values (2, 'bbb'); insert into tab1 (ID, name) values (3, 'ccc'); select * from tab1; 123456789101112131415

mysql安装与使用教程(超详细MySQL安装及基本使用教程)

  • 使用ER工具,点击File->New Model->Add diagram,绘制数据库概念模式图

使用python连接mysql

  • 安装了python connector
  • 使用pip install mysql安装python库MySQLdb
  • 测试连接import MySQLdb # 打开数据库连接 url,username,password,database db = MySQLdb.connect("localhost","lyp1234","1234","test" ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # 使用execute方法执行SQL语句 cursor.execute("SELECT * from tab1") # 使用 fetchone() 方法获取一条数据 data = cursor.fetchall() for d in data: print(d) # 关闭数据库连接 db.close() 12345678910111213141516171819
  • 测试结果:

使用C++连接mysql

  • 安装了c++ connector
  • 使用网上的测试代码,连接mysql数据库执行select * from tab1命令#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mysql.h" //所需头文件 using namespace std; int main() { const char host[] = "localhost"; //MySQL服务器IP地址;若是本地可填写“localhost”或127.0.0.1 const char user[] = "lyp1234"; //MySQL的用户名 const char pswd[] = "1234"; //密码 const char table[] = "test"; //数据库名称 unsigned int port = 3306; //MySQL服务端口号,默认是3306 MYSQL myCont;//创建MYSQL对象,定义数据库连接句柄 MYSQL_RES *result;//查询结果集,存放查询结果 MYSQL_ROW sql_row;//存放一行查询结果的字符串数组 MYSQL_FIELD *fd;//包含字段信息的结构 char column[32][32]; int res; mysql_library_init(0,NULL,NULL);//初始化MySQL库 mysql_init(&myCont);//初始化连接处理程序 if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0)) {//通过调用mysql_real_connect()连接到服务器 cout<<"connect succeed!"<<endl; mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文 res=mysql_query(&myCont,"select * from tab1");//执行查询语句,mysql_query如果查询成功,零;如果出现一个错误,非零。 if(!res) { result=mysql_store_result(&myCont);//保存查询到的数据到result if(result) { int i,j; cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl; for(i=0;fd=mysql_fetch_field(result);i++)//获取列名 { strcpy(column[i],fd->name); } j=mysql_num_fields(result); for(i=0;i<j;i++) { printf("%st",column[i]); } printf("n"); while(sql_row=mysql_fetch_row(result))//获取具体的数据 { for(i=0;i<j;i++) { printf("%st",sql_row[i]); } printf("n"); } } } else { cout<<"query sql failed!"<<endl; } } else { cout<<"connect failed!"<<endl; } //注意用完数据库要及时回收资源 if(result!=NULL) mysql_free_result(result);//释放结果资源 mysql_close(&myCont);//关闭MySQL连接 mysql_library_end();//关闭MySQL库 return 0; } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  • 从mysql安装目录(我的是C:Program FilesMySQLMySQL Server 8.0)中拷贝libmysql.dll和libmysql.lib到工程目录下
  • gcc编译,使用命令g++ -I 'C:Program FilesMySQLMySQL Server 8.0include' -L 'C:Program FilesMySQLMySQL Server 8.0lib' test.cc -llibmysql -o test编译,命令中指定了mysql 安装目录下的lib和include 文件,注意填写你对应的文件夹。
  • 执行结果
  • linux系统下应该就是mysql安装目录不同,编译时注意修正即可。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论