1. 概述
Relational database like MariaDB is one of the required components to setup a web server as well as other less common uses such as when configuring a shared Kodi database. On Arch Linux MySQL has been replaced by a functionally identical community fork called MariaDB. The installation is also practically identical and as simple as can be expected.
2. 安裝 MySQL / MariaDB
Install with pacman.
sudo pacman -S mariadb
Initialize data directories.
> sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is mysql@localhost, it has no password either, but
you need to be the system 'mysql' user to connect.
After connecting you can set the password, if you would need to be
able to connect as any of these users with a password and without sudo
See the MariaDB Knowledgebase at https://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl
Please report any problems at https://mariadb.org/jira
The latest information about MariaDB is available at https://mariadb.org/.
You can find additional information about the MySQL part at:
https://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
By default MySQL will run as the user running the command unless a user is explicitly
specified with --user option. Path to the installation directory can be specified with --basedir option while data directory is specified with --datadir.
3. 運行 MySQL / MariaDB
Start the service with systemd & Enable the service to start on boot.
sudo systemctl start mysqld #啓動
sudo systemctl enable mysqld #開機啓動
Then reboot!
4. 配置 MySQL / MariaDB
Secure the installation.
sudo mysql_secure_installation
Make sure you
set root password
,remove anonymous users
anddisallow root login remotely
unless you know you will need remote access to MariaDB. There should also be no need to keep test database soremove test database and access
to it. Finish the install process withreload privilege tables now
.
5. 使用 MySQL / MariaDB
Invoke the command line tool.
The username is specified with
-u
option follower by the username which isroot
by default. The password is specified with the-p
option followed by the password without a space in between or the password can be omitted in which case MariaDB will prompt for one.
-
List all existing databases.
SHOW DATABASES;
-
List all database users.
SELECT DISTINCT User FROM mysql.user;
Use command line or Install phpMyAdmin to administer MySQL / MariaDB databases.
通過下面命令檢查之後,如果看到有mysql 的socket處於 listen 狀態則表示安裝成功:
sudo netstat -tap | grep mysql
登陸mysql數據庫可以通過如下命令:
mysql -u root -p
-u 表示選擇登陸的用戶名, -p 表示登陸的用戶密碼,上面命令輸入之後會提示輸入密碼,此時輸入密碼就可以登錄到mysql。
下面是一些命令行中操作的DEMO,可做今後參考:
mysqladmin -u root -p create blog
mysql mysql -u root -p
show databases;
use blog;
CREATE TABLE IF NOT EXISTS `blog_table`(
`blogId` BIGINT UNSIGNED,
`url` VARCHAR(100) NOT NULL,
`title` VARCHAR(1000) NOT NULL,
`support` INT UNSIGNED,
`pageView` INT UNSIGNED,
PRIMARY KEY ( `blogId` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tag_table`(
`tagId` INT UNSIGNED AUTO_INCREMENT,
`tagName` VARCHAR(100) NOT NULL,
PRIMARY KEY ( `tagId` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `blog_tag_relation_table`(
`relationId` INT UNSIGNED AUTO_INCREMENT,
`blogId` BIGINT UNSIGNED,
`tagId` INT UNSIGNED,
PRIMARY KEY ( `relationId` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
show tables;
desc blog_table;
desc tag_table;
desc blog_tag_relation_table;
//change blogId int 2 bigint
alter table blog_table change blogId blogId BIGINT UNSIGNED;
//show data
select * from blog_table;
//delete data
delete from blog_table where blogId=201801021423;
INSERT INTO blog_table(blogId,url,title,support,pageView)
VALUES(201801021423,'http://106.14.226.191:3000/blog/201607281658.html','[商業_法務] 1、公司一款新消費類電子產品如何快速全面的專利保護',0,0);
//too short
alter table blog_table change title title VARCHAR(1000) NOT NULL;
INSERT INTO tag_table(tagId,tagName)
VALUES(0,'硬件_模擬電路');
select * from blog_table;
select * from tag_table;
select * from blog_tag_relation_table;
delete from blog_table where blogId>0;
delete from tag_table where tagId>=0;
delete from blog_tag_relation_table where relationId >= 0;
select a.title , a.url, b.tagName from blog_table a, tag_table b, blog_tag_relation_table c WHERE a.blogId = c.blogId AND a.blogId = 201602021408 AND b.tagId = c.tagId;
select a.title , a.url, b.tagName from blog_table a, tag_table b, blog_tag_relation_table c WHERE a.blogId = c.blogId AND b.tagId = c.tagId ORDER BY b.tagId;
爲了python操作mysql需要執行下面命令:
pip install MySQL-python (arch linux 2.7 版本 python 會報錯,建議用下面的命令)
sudo pacman -S mysql-python
鏈接
: ** 在 ubuntu 上比較好操作,在 arch linux 上有些難操作,這裏記錄下~ **