From ElectroDragon
Revision as of 12:44, 28 January 2020 by Chao (talk | contribs)
Jump to navigation Jump to search


  • sudo apt-get install mysql-server
  • sudo mysql_secure_installation -> after set root pass, all the rest set to Y


  • apt-get install mysql-server mysql-client

You will be asked to provide a password for the MySQL root user - this password is valid for the user root@localhost as well as, so we don't have to specify a MySQL root password manually later on.

Getting MySQL Support In PHP5

To get MySQL support in PHP, we can install the php5-mysql package. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

apt-cache search php5

Pick the ones you need and install them like this:

apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Xcache is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and APC. It is strongly recommended to have one of these installed to speed up your PHP page.

Xcache can be installed as follows:

apt-get install php5-xcache

and let's restart the php by doing:

service php5-fpm restart

Now we should see many extra modules you installed in the phpinfo.php, including the mysql.



  • Update: UPDATE mysql.user SET Password = PASSWORD('password') WHERE User = 'root';



create tables

  • normal: CREATE TABLE stock (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), date DATE(255), share INT(100));
  • datetime, varchar, float, int: CREATE TABLE stock (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), date2 Datetime, price FLOAT(50,3), share INT(100));
  • CREATE TABLE sum1 (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), total_share INT(100), total_cost FLOAT(50,3), avg_price FLOAT(50,3));

show tables

  • show tables;

table insert

  • insert into stock(name, date2, share) values ('DBC', '19600101', 1);
  • insert into stock(name, date2, share) values ('DBC', '1960-01-01', 2);

example data

  • insert into stock(name, date2, share, price) values ('DBC', now(), 1, 14.3);
  • insert into stock(name, date2, share, price) values ('DBC', now(), 2, 14.87);


  • insert ignore into sum1 (name) values ('test2')

table drop

  • drop table stock;

table select

  • SELECT * FROM stock.stock;
  • SELECT User, Host, authentication_string FROM mysql.user;
INSERT INTO sum2(name, total_share, total_cost, avg_price)
SELECT name, 
SUM(share) 'total_share', 
round(SUM(price * share),2) 'total_cost', 
round(SUM(price * share)/SUM(share),2) 'avg_price' 
FROM stock 
GROUP BY name;

table delete

  • DELETE FROM stock WHERE name = 'DBC';
  • DELETE FROM stock WHERE ID = 3;


Content Update

  • update stock set date2='20190401' where id=29;
  • update stock, sum1 set sum1.total_share= (select SUM(share) from stock where;
  • update stock, sum1 set sum1.total_cost= (select round(SUM(price * share),2) from stock where;
  • update stock, sum1 set sum1.avg_price= (select round(SUM(price * share)/SUM(share),2) from stock where;

Column Update

  • Add column

ADD COLUMN last_buy VARCHAR(15);

ADD COLUMN last_buy Datetime;

alter table sum1 
add column last_price float(50,3);

  • drop
alter table stock 
drop column last_buy;

Other operations

  • Make change: FLUSH PRIVILEGES;
  • service mysql restart
  • systemctl start mysql
  • systemctl enable mysql

Output - dump

  • mysqldump -u root -p test>test.sql


  • mysql -u root -p
  • create database stock
  • exit
  • mysql -u root -p stock < stock.sql


Example for mediawiki:

  • show databases;
  • use edwik;
  • select * from page where page_title='RISC-V';
  • delete from page where page_id=4975;


dump sql

  • docker exec db sh -c 'exec mysqldump -uroot -ppassword wordpress' > /web/import/test2.sql

import sql

  • docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /web/import/blogdb.sql