Once you have more than one keyspace, with tables distributed across both keyspaces, your application may not know how to properly route queries to the correct keyspace.
If you originally set up your application configuration code with something like DATABASE_NAME=your_database_name, where your_database_name is the name of your original unsharded keyspace, you will need to update your configuration code so that all queries don’t go straight to that keyspace.The preferred way to do this is to just leave off the database name completely in your application configuration code. PlanetScale will be able to route traffic correctly just using the connection username and password.While this is the preferred way, it’s sometimes not possible. For example, many frameworks and ORMs require that you include a database name.In those cases, you should use @primary. This will send any incoming queries first to our Global Edge Network, which will see that you’re targeting a primary. Edge will then send the request to the VTGate(s)/load balancer. We typically will use Vitess’s Global Routing to direct the query to the correct keyspace and, optionally, correct shard.
NoteIf you explicitly wish to target a replica for some or all reads, using @replica will have the same effect as @primary in that it will automatically route the request to the correct keyspace.
Global Replica Credentials are not currently supported in this context. You can still target replicas instead of your primary with @replica, but it will not automatically route the query to the closest replica.
Using @primary is simple, but there are slight variations for each framework. The following code snippets show how to target @primary in some of the popular languages/frameworks. If you don’t see your framework on here and are unsure of how to proceed, please reach out to support.
# .envDATABASE_HOST=aws.connect.psdb.cloudDATABASE=@primaryDATABASE_USERNAME=usernameDATABASE_PASSWORD=password# index.php// Use env variables to connect to the database$dsn = "mysql:host={$_ENV["DATABASE_HOST"]};dbname={$_ENV["DATABASE"]}";$options = array( PDO::MYSQL_ATTR_SSL_CA => "/etc/ssl/cert.pem",);$pdo = new PDO($dsn, $_ENV["DATABASE_USERNAME"], $_ENV["DATABASE_PASSWORD"], $options);