Skip to content

Commit d89d75d

Browse files
dev_server: Add password
Also fix up the QUICKSTART document for other recent changes made.
1 parent 0b8d523 commit d89d75d

3 files changed

Lines changed: 122 additions & 45 deletions

File tree

villagesql/dev_server/QUICKSTART.md.template

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,48 @@ brew install openssl
2525
# 2. Start the server
2626
./villagesql start
2727

28-
# 3. Connect to the server
28+
# 3. Check the server is running
29+
./villagesql status
30+
31+
# 4. Connect to the server
2932
./villagesql connect
3033

31-
# 4. Stop the server (when done)
34+
# 5. Stop the server (when done)
3235
./villagesql stop
3336
```
3437

35-
## Installing Extensions
36-
37-
VillageSQL extensions are installed by name. The server searches for `.veb` files in configured directories (default: `lib/veb/`).
38+
To set a root password, pass `--password` to `init` — you will be prompted interactively:
3839

39-
```sql
40-
-- Install an extension by name (looks for extension_name.veb)
41-
INSTALL EXTENSION extension_name;
40+
```bash
41+
./villagesql init --password
42+
./villagesql start
4243
```
4344

44-
### Example Extensions Included
45+
The password is stored in `instances/default/my.cnf` (mode 600) and picked up automatically by `connect` and `stop`.
4546

46-
This package includes two example extensions you can try immediately:
47+
## Managing Extensions
4748

48-
```sql
49-
INSTALL EXTENSION vsql_simple;
50-
INSTALL EXTENSION vsql_complex;
51-
```
49+
Extension management has two steps: copy the `.veb` file into the instance, then install it in the running server via SQL.
50+
51+
### Adding and removing .veb files
52+
53+
```bash
54+
# Copy a .veb file into this instance's extension directory
55+
./villagesql veb add /path/to/my_extension.veb
5256

53-
**Tip:** Place your `.veb` files in `lib/veb/` to make them discoverable by the server.
57+
# List .veb files available to this instance
58+
./villagesql veb ls
5459

55-
### Verifying Installation
60+
# Remove a .veb file from this instance
61+
./villagesql veb rm my_extension
62+
```
63+
64+
### Installing extensions in the running server
5665

5766
```sql
67+
-- Install an extension (must be in the instance's veb directory)
68+
INSTALL EXTENSION my_extension;
69+
5870
-- List installed extensions
5971
SELECT * FROM INFORMATION_SCHEMA.EXTENSIONS;
6072

@@ -63,14 +75,22 @@ CREATE TABLE test_table (
6375
id INT PRIMARY KEY,
6476
custom_col YOUR_CUSTOM_TYPE
6577
);
78+
79+
-- Uninstall an extension
80+
UNINSTALL EXTENSION my_extension;
6681
```
6782

68-
### Uninstalling
83+
### Example Extensions Included
84+
85+
This package includes two example extensions seeded into new instances automatically:
6986

7087
```sql
71-
UNINSTALL EXTENSION your_extension_name;
88+
INSTALL EXTENSION vsql_simple;
89+
INSTALL EXTENSION vsql_complex;
7290
```
7391

92+
**Tip:** `.veb` files placed in `lib/veb/` are automatically copied into new instances when you run `./villagesql init`.
93+
7494
## Directory Structure
7595

7696
- `bin/` - Server and client binaries
@@ -91,7 +111,7 @@ The helper script uses these defaults:
91111
- **Port:** 3307 (configurable via `$MYSQL_PORT`)
92112
- **Bind address:** `127.0.0.1` (localhost only)
93113
- **Error log:** `<instance-dir>/data/error.log`
94-
- **Root user:** `root` (no password)
114+
- **Root user:** `root` (no password by default; set one with `init --password`)
95115

96116
## Multiple Instances
97117

@@ -104,14 +124,24 @@ socket, and PID files don't conflict:
104124
./villagesql --dir ./instance-a start
105125

106126
# Instance B on port 3308
107-
MYSQL_PORT=3308 ./villagesql --dir ./instance-b init
127+
./villagesql --dir ./instance-b init
108128
MYSQL_PORT=3308 ./villagesql --dir ./instance-b start
109129

110130
# Connect to each independently
111131
./villagesql --dir ./instance-a connect
112132
./villagesql --dir ./instance-b connect
113133
```
114134

135+
Use `--here` as a shorthand to create an instance in the current directory
136+
(equivalent to `--dir $(pwd)/.vsql_here`):
137+
138+
```bash
139+
cd /path/to/my-project
140+
./villagesql --here init
141+
./villagesql --here start
142+
./villagesql --here connect
143+
```
144+
115145
## Manual Server Control
116146

117147
If you prefer manual control or need custom options:
@@ -167,12 +197,14 @@ UNINSTALL EXTENSION extension_name;
167197

168198
Check the error log:
169199
```bash
170-
tail -50 ./data/error.log
200+
tail -50 instances/default/data/error.log
201+
# Or for a custom instance directory:
202+
tail -50 <instance-dir>/data/error.log
171203
```
172204

173205
Common issues:
174206
- Port 3307 already in use: Set `MYSQL_PORT=3308` before running `./villagesql start`
175-
- Stale socket file: Remove `mysql.sock` manually
207+
- Stale socket file: Remove `instances/default/mysql.sock` manually (or `<instance-dir>/mysql.sock`)
176208
- Permissions: Ensure files are readable/executable
177209

178210
### Can't Connect

villagesql/dev_server/TEST_DOCS.md.template

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,24 @@ This package includes the mysql-test framework (`mysql-test/`) for extension aut
1010
When developing an extension, specify your test suite directory and VEB location:
1111

1212
```bash
13-
cd mysql-test
14-
1513
# Run all tests in your extension's test suite
16-
./mysql-test-run.pl --suite=/path/to/your/extension/tests \
17-
--veb-source-dir=/path/to/your/extension/build
14+
./villagesql mysql-test --suite=/path/to/your/extension/tests \
15+
--veb-source-dir=/path/to/your/extension/build
1816

1917
# Run with parallel execution
20-
./mysql-test-run.pl --suite=/path/to/your/extension/tests \
21-
--veb-source-dir=/path/to/your/extension/build \
22-
--parallel=auto
18+
./villagesql mysql-test --suite=/path/to/your/extension/tests \
19+
--veb-source-dir=/path/to/your/extension/build \
20+
--parallel=auto
2321

24-
# Run a specific test (test name as positional argument)
25-
./mysql-test-run.pl --suite=/path/to/your/extension/tests \
26-
--veb-source-dir=/path/to/your/extension/build \
27-
my_test
22+
# Run a specific test
23+
./villagesql mysql-test --suite=/path/to/your/extension/tests/my_test.test \
24+
--veb-source-dir=/path/to/your/extension/build
25+
my_test
2826

2927
# Record test results (for creating .result files)
30-
./mysql-test-run.pl --suite=/path/to/your/extension/tests \
31-
--veb-source-dir=/path/to/your/extension/build \
32-
--record
28+
./villagesql mysql-test --suite=/path/to/your/extension/tests \
29+
--veb-source-dir=/path/to/your/extension/build \
30+
--record
3331
```
3432

3533
**Options:**
@@ -41,13 +39,13 @@ cd mysql-test
4139
### Test Organization
4240

4341
Create your extension tests in:
44-
- `mysql-test/suite/my_extension/` - Your test suite
45-
- `mysql-test/suite/my_extension/t/` - Test files (.test)
46-
- `mysql-test/suite/my_extension/r/` - Expected results (.result)
42+
- `tests/` - Your test suite
43+
- `tests/t/` - Test files (.test)
44+
- `tests/r/` - Expected results (.result)
4745

4846
Example test file structure:
4947
```
50-
mysql-test/suite/my_extension/
48+
tests/
5149
├── t/
5250
│ ├── basic.test
5351
│ └── advanced.test

villagesql/dev_server/villagesql.sh

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ done
3636
cmd_init() {
3737
set -e
3838

39+
# Parse init-specific flags
40+
local password=""
41+
while [[ $# -gt 0 ]]; do
42+
case "$1" in
43+
--password)
44+
read -r -s -p "Password for root: " password
45+
echo
46+
local confirm
47+
read -r -s -p "Confirm password: " confirm
48+
echo
49+
if [[ "$password" != "$confirm" ]]; then
50+
echo "Error: Passwords do not match"
51+
exit 1
52+
fi
53+
shift
54+
;;
55+
*)
56+
echo "Error: Unknown init option: $1"
57+
exit 1
58+
;;
59+
esac
60+
done
61+
3962
# Create the instance directory if it doesn't exist yet
4063
mkdir -p "$DIR"
4164

@@ -67,6 +90,16 @@ cmd_init() {
6790
--datadir="$datadir" \
6891
--log-error-verbosity=3
6992

93+
if [[ -n "$password" ]]; then
94+
# Write SQL to set the root password; applied by cmd_start on first boot.
95+
# Use a subshell with umask 077 so the file is created 600 from the start.
96+
local escaped="${password//\'/\'\'}"
97+
(umask 077; printf "ALTER USER 'root'@'localhost' IDENTIFIED BY '%s';\n" "$escaped" > "$DIR/init_password.sql")
98+
99+
# Write per-instance client config so connect/stop pick up credentials automatically.
100+
(umask 077; printf '[client]\nuser=root\npassword=%s\n' "$password" > "$DIR/my.cnf")
101+
fi
102+
70103
echo ""
71104
echo "✓ Database initialized successfully!"
72105
echo ""
@@ -117,6 +150,11 @@ cmd_start() {
117150
echo " Log: $logfile"
118151
echo ""
119152

153+
local init_file_args=()
154+
if [[ -f "$DIR/init_password.sql" ]]; then
155+
init_file_args=("--init-file=$DIR/init_password.sql")
156+
fi
157+
120158
"$BASEDIR/bin/mysqld" \
121159
--no-defaults \
122160
--basedir="$BASEDIR" \
@@ -127,13 +165,15 @@ cmd_start() {
127165
--bind-address=127.0.0.1 \
128166
--pid-file="$pidfile" \
129167
--log-error="$logfile" \
130-
--log-error-verbosity=3 &
168+
--log-error-verbosity=3 \
169+
"${init_file_args[@]}" &
131170

132171
local server_pid=$!
133172
echo "Server starting (PID: $server_pid)..."
134173

135174
for i in {1..30}; do
136175
if "$BASEDIR/bin/mysqladmin" --socket="$socket" ping >/dev/null 2>&1; then
176+
rm -f "$DIR/init_password.sql"
137177
echo ""
138178
echo "✓ Server is ready!"
139179
echo ""
@@ -167,7 +207,10 @@ cmd_connect() {
167207
exit 1
168208
fi
169209

170-
exec "$BASEDIR/bin/mysql" --socket="$socket" -u root "$@"
210+
local auth_args=(-u root)
211+
[[ -f "$DIR/my.cnf" ]] && auth_args=("--defaults-file=$DIR/my.cnf")
212+
213+
exec "$BASEDIR/bin/mysql" "${auth_args[@]}" --socket="$socket" "$@"
171214
}
172215

173216
cmd_status() {
@@ -228,8 +271,11 @@ cmd_stop() {
228271
exit 1
229272
fi
230273

274+
local auth_args=(-u root)
275+
[[ -f "$DIR/my.cnf" ]] && auth_args=("--defaults-file=$DIR/my.cnf")
276+
231277
echo "Stopping VillageSQL server (PID: $pid)..."
232-
"$BASEDIR/bin/mysqladmin" --socket="$socket" -u root shutdown 2>/dev/null || true
278+
"$BASEDIR/bin/mysqladmin" "${auth_args[@]}" --socket="$socket" shutdown 2>/dev/null || true
233279

234280
for i in {1..30}; do
235281
if ! kill -0 "$pid" 2>/dev/null; then
@@ -336,7 +382,7 @@ cmd_help() {
336382
echo "Usage: villagesql [--dir <path>] <command> [args]"
337383
echo ""
338384
echo "Commands:"
339-
echo " init Initialize the database (run once before first use)"
385+
echo " init [--password] Initialize the database (run once before first use)"
340386
echo " start Start the VillageSQL server"
341387
echo " connect Connect to the running server (extra args passed to mysql)"
342388
echo " stop Stop the VillageSQL server"
@@ -357,6 +403,7 @@ cmd_help() {
357403
echo ""
358404
echo "Examples:"
359405
echo " ./villagesql init && ./villagesql start"
406+
echo " ./villagesql init --password && ./villagesql start"
360407
echo " ./villagesql --dir /tmp/test-instance init"
361408
echo " ./villagesql --dir /tmp/test-instance start"
362409
echo " MYSQL_PORT=3308 ./villagesql --dir ./second start"
@@ -371,7 +418,7 @@ case "$DIR_FLAG" in
371418
esac
372419

373420
case "$1" in
374-
init) cmd_init ;;
421+
init) shift; cmd_init "$@" ;;
375422
start) cmd_start ;;
376423
connect) shift; cmd_connect "$@" ;;
377424
stop) cmd_stop ;;

0 commit comments

Comments
 (0)