
首先,这个项目的多数据源配置建立在mysql已经搭建完成的基础上,这部分的源代码github上都有,网上也有不少教程,我就不作说明了,直接讲如何配置oracle。
oracle我用的版本是11g,具体版本是11.2.0.1.0。下载及安装,建表过程省略(详情请百度,建表推荐用pl/sql)
我的项目中,数据库的账号密码都是通过properties文件配置的,首先你需要配置好spring项目中的oracle的url,账号以及密码。
dataSource2.driver=oracle.jdbc.driver.OracleDriver
dataSource2.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
dataSource2.username=sys as sysdba
dataSource2.password=a123456
driver表示驱动名,username表示登录oracle的用户名,这里需要注意的是oracle非常注重数据库管理员的权限,因此在登录时需要指明登录者的身份,pl/sql以及navicat会让你选择以什么角色登录,而在spring项目中访问时,直接输入 用户名 + as sysdba 的形式作为用户名,密码就是先前安装oracle时设置的密码。
接下来配置spring的配置文件,来配置2个数据源,我主要是参考了网上的文章,大致原理是为不同数据源配置不同的sqlSessionFactory以及sqlSessionTemplate,为项目编写工具类,通过设置不同的键值切换数据源。
MySQL 5.7 新增默认账号 mysql.session和mysql.syshttps://www.jianshu.com/p/427cac0d8763Privileges Supported by MySQLhttps://dev.mysql.com/doc/refman/5.7/en/grant.htmlMySQL 5.7 Reference Manual/Reserved Accountshttps://dev.mysql.com/doc/refman/5.7/en/reserved-users.htmlWhat are mysql.session@localhost and mysql.sys@localhost user accounts good for?https://stackoverflow.com/questions/46149220/what-are-mysql-sessionlocalhost-and-mysql-syslocalhost-user-accounts-good-forOne part of the MySQL installation process is data directory initialization (see Section 2.10.1, “Initializing the Data Directory” ). During data directory initialization, MySQL creates user accounts that should be considered reserved:'root'@'localhost : Used for administrative purposes. This account has all privileges and can perform any operation. Strictly speaking, this account name is not reserved, in the sense that some installations rename the root account to something else to avoid exposing a highly privileged account with a well-known name.'mysql.sys'@'localhost' : Used as the DEFINER for sys schema objects. Use of the mysql.sys account avoids problems that occur if a DBA renames or removes the root account. This account is locked so that it cannot be used for client connections.'mysql.session'@'localhost' : Used internally by plugins to access the server. This account is locked so that it cannot be used for client connections.用于sys schema中对象的定义。使用mysql.sys用户可避免DBA重命名或者删除root用户时发生的问题。该用户已被锁定,客户端无法连接。插件内部使用来访问服务器。该用户已被锁定,客户端无法连接。root账号,其用于管理。该用户拥有所有权限,可执行任何 *** 作。 root是MySQL的特权账号,这个众所周知,也带来安全隐患,建议将root账号禁用或者删除,新建一个特权账号用于管理。欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)