create database if not exists temp; use temp; drop database if exists MY_STUPID_DATABASE; create database if not exists MY_STUPID_DATABASE; use MY_STUPID_DATABASE; create table if not exists people (id int, name varchar(64)); create table if not exists charges (id int, people_id int, amount int); insert into people values (1, 'mark'), (2, 'john'), (3, 'you'); insert into charges (people_id, amount) values (1, 10), (2,10), (1, 3), (1, 4), (2,5); select count(1) from people; select count(1) from charges; create user mark@localhost; ALTER USER mark@localhost IDENTIFIED BY 'mark'; grant all privileges on *.* to mark@localhost; show grants for mark@localhost;
Start mysql as: mysql -u root -p
or if not password : mysql -u root
Execute the commands:
use MY_STUPID_DATABASE; source my_stuff.sql
tee error.log use database DB; select now(), @@hostname, USER(); begin; # Your commands, not schema changes. commit; # Now do your schema changes OUTSIDE of any commit block. # You may have several commit blocks, schema changes should be outside of them.
This technique can be used for almost all schema changes. There are some exceptions, like if triggers exist. Also, some versions make only work with some versions of MySQL.
This is used a lot and be used with sql change files.
Save to a file called: "my_stuff2.sql"
tee error2.log use database MY_STUPID_DATABASE ### Yes I did use a second time.; select now(), @@hostname, USER(); create table if not exists BLAH (i int); alter table BLAH add column i2 int; ALTER TABLE charges ADD COLUMN ignore_me1 int, ALGORITHM=INSTANT; ALTER TABLE charges ADD COLUMN ignore_me2 int; begin; insert into charges (people_id, amount) values (1,1), (2,2), (1,3), (2,4); commit; alter table people CHANGE id id INT AUTO_INCREMENT PRIMARY KEY; alter table charges CHANGE id id INT AUTO_INCREMENT PRIMARY KEY; show create table charges; select count(1) from charges; show tables;
Start mysql: In mysql shell: mysql -v -v -v -u mark -pmark MY_STUPID_DATABASE
Execute commands in mysql:
use MY_STUPID_DATABASE source my_stuff2.sql
Look at error log with linux command: grep -vi err error2.log
Save to a file called "stuff.bash ".
date whoami set PTDEBUG =1; pt-online-schema-change --dry-run --alter "ADD COLUMN ignore_me_pt1 INT" D=MY_STUPID_DATABASE,t=charges -u mark -pmark; date pt-online-schema-change --dry-run --alter "ADD index name (name)" D=MY_STUPID_DATABASE,t=people -u mark -pmark; date pt-online-schema-change --dry-run --alter "ADD index id_amount (id, amount)" D=MY_STUPID_DATABASE,t=charges -u mark -pmark; date # you should really check the dry runs and then do the commits. pt-online-schema-change --execute --alter "ADD COLUMN ignore_me_pt1 INT" D=MY_STUPID_DATABASE,t=charges -u mark -pmark; date pt-online-schema-change --execute --alter "ADD index name (name)" D=MY_STUPID_DATABASE,t=people -u mark -pmark; date pt-online-schema-change --execute --alter "ADD index id_amount (id, amount)" D=MY_STUPID_DATABASE,t=charges -u mark -pmark; date mysql -v -v -v -u mark -pmark MY_STUPID_DATABASE -e "show create table people;" mysql -v -v -v -u mark -pmark MY_STUPID_DATABASE -e "show create table charges;"
Execute file as: " bash stuff.bash > stuff.log 2>&1 "
Look at error file: "more stuff.log"