Ubuntu20.04使用c++连接MySQL8.0(Connector/C++ 8.0)
一、安装Mysql
1.方法一(本人用的方法二):
1.1在官网下载mysql安装包
https://downloads.mysql.com/archives/community/
1.2解压文件(进入下载文件夹的路径下):
tar -xvf mysql-server_5.7.13-1ubuntu16.04_i386.deb-bundle.tar
- 解压了这个包之后会在文件夹看到多个deb文件
1.3安装
libmysqlclient20_5.7.13-1ubuntu16.04_i386.deb
libmysqlclient-dev_5.7.13-1ubuntu16.04_i386.deb
libmysqld-dev_5.7.13-1ubuntu16.04_i386.deb
mysql-common_5.7.13-1ubuntu16.04_i386.deb
mysql-community-source_5.7.13-1ubuntu16.04_i386.deb
mysql-community-client_5.7.13-1ubuntu16.04_i386.deb
mysql-community-server_5.7.13-1ubuntu16.04_i386.deb
mysql-server_5.7.13-1ubuntu16.04_i386.deb
sudo dpkg -i libmysqlclient20_5.7.13-1ubuntu16.04_i386.deb libmysqlclient-dev_5.7.13-1ubuntu16.04_i386.deb libmysqld-dev_5.7.13-1ubuntu16.04_i386.deb mysql-common_5.7.13-1ubuntu16.04_i386.deb mysql-community-source_5.7.13-1ubuntu16.04_i386.deb mysql-community-client_5.7.13-1ubuntu16.04_i386.deb mysql-community-server_5.7.13-1ubuntu16.04_i386.deb mysql-server_5.7.13-1ubuntu16.04_i386.deb
- 也可以一个一个来安装(这样子能够搞懂依赖的关系)
安装过程中可能缺少依赖,所以可以用:
sudo apt-get install [文件名]
- eg:这里面的问题是缺少libaio1和libmecab2,所以可以用:
sudo apt-get install libaio1 libmecab2
- 如果还是不能安装使用:
sudo apt-get -f install
- 安装完成之后,查看mysql服务状态:
service mysql start
- 进入MySQL
mysql -u root -p
方法二、通过apt安装MYSQL服务(本人用的该方法)
#命令1 更新源
sudo apt-get update
#命令2 安装mysql服务
sudo apt-get install mysql-server
2.1 初始化配置
sudo mysql_secure_installation
- 配置项较多,如下所示:
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (选择N ,不会进行密码的强校验)
#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)
#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (选择N,不删除匿名用户)
#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N (选择N,允许root远程连接)
#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (选择N,不删除test数据库)
#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (选择Y,修改权限立即生效)
2.2 检查mysql服务状态
systemctl status mysql.service
3.1 配置远程访问
- 在Ubuntu下MySQL缺点是只允许本地访问的,使用workbench连接工具是连不上的;如果你要其他机器也能够访问的话,需要进行配置;
- 找到 bind-address 修改值为 0.0.0.0(如果需要远程访问)
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf #找到 bind-address 修改值为 0.0.0.0(如果需要远程访问)
sudo /etc/init.d/mysql restart #重启mysql
二、连接
参考博客:https://blog.csdn.net/donotgogentle/article/details/106748737
https://blog.csdn.net/mazamu/article/details/120677439
2.1 安装boost库
sudo apt-get install libboost-all-dev
2.2 安装 libmysqlclient 库
sudo apt-get install libmysqlclient-dev
2.3 安装C++Connector 库
安装连接:https://dev.mysql.com/downloads/connector/cpp/
本人前三个都安装了
【注:使用方法二安装MySQL时,安装C++Connector会缺少依赖,在方法一中找到缺少依赖的安装,将其安装】