linux下安裝配置mysql

(1)yum安裝方法:

yum install mariadb mariadb-server mariadb-libs -y     # linux 7.x環境

yum install mysql-server mysql-devel mysql-libs -y   # linux 6.x環境

(2)源碼安裝:

環境:Redhat 7.4  、mysql-5.5.20

tar -zxvf mysql-5.5.20.tar.gz  #解壓

cd mysql-5.5.20  #進到目錄

yum install gcc gcc-c++ cmake ncurses-devel ncurses -y  #安裝依賴包,ncurses是字符處理庫

#從MySQL5.5版本開始,MySQL源碼安裝的編譯工具由configure開始向cmake過渡

[root@xsbfwbtest mysql-5.5.20]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql55/ \   #預編譯路徑
> -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \   #socket通信文件
> -DMYSQL_DATADIR=/software/mysql \     #數據存儲路徑
> -DSYSCONFDIR=/etc \                   #配置文件路徑
> -DMYSQL_USER=mysql \                  #運行用戶
> -DMYSQL_TCP_PORT=3306 \               #運行時的監聽端口
> -DWITH_PARTITION_STORAGE_ENGINE=1 \   #開啓PARTITION引擎
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \   #開啓BLACKHOLE引擎
> -DWITH_XTRADB_STORAGE_ENGINE=1 \      #開啓XTRADB引擎
> -DWITH_MYISAM_STORAGE_ENGINE=1 \      #開啓MYISAM引擎
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \    #開啓INNOBASE引擎
> -DWITH_READLINE=1 \                   #打開快捷鍵功能
> -DENABLED_LOCAL_INFILE=1 \            #允許從本地導數據
> -DWITH_EXTRA_CHARSETS=1 \             #支持額外字符集
> -DDEFAULT_CHARSET=utf8 \              #默認字符集
> -DDEFAULT_COLLATION=utf8_general_ci \ #檢驗字符
> -DEXTRA_CHARSETS=all \                #加載所有擴展字符集
> -DWITH_BIG_TABLES=1 \                 #將臨時表存儲到磁盤
> -DWITH_DEBUG=0                        #禁止調試模式
[root@xsbfwbtest mysql-5.5.20]# make  #編譯
......
[root@xsbfwbtest mysql-5.5.20]# make install  #安裝
......

 

[root@xsbfwbtest mysql-5.5.20]# cd /usr/local/mysql55/
[root@xsbfwbtest mysql55]# \cp support-files/my-large.cnf /etc/my.cnf
[root@xsbfwbtest mysql55]# \cp support-files/mysql.server /etc/init.d/mysqld
[root@xsbfwbtest mysql55]# chkconfig --add mysqld
[root@xsbfwbtest mysql55]# chkconfig --level 35 mysqld on
[root@xsbfwbtest mysql55]# mkdir -p /software/mysql
[root@xsbfwbtest mysql55]# useradd mysql
[root@xsbfwbtest mysql55]# ln -s /usr/local/mysql55/bin/* /usr/bin/
[root@xsbfwbtest mysql55]# /usr/local/mysql55/scripts/mysql_install_db --user=mysql --datadir=/software/mysql/ --basedir=/usr/local/mysql55/
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/local/mysql55//bin/mysqladmin -u root password 'new-password'
/usr/local/mysql55//bin/mysqladmin -u root -h xsbfwbtest password 'new-password'

Alternatively you can run:
/usr/local/mysql55//bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr/local/mysql55/ ; /usr/local/mysql55//bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql55//mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/local/mysql55//scripts/mysqlbug script!

[root@xsbfwbtest mysql55]# service mysqld start   #上面已經設置爲系統服務,所以直接啓動
Starting MySQL... SUCCESS!
[root@xsbfwbtest mysql55]# service mysqld status   #mysql已經啓動了
 SUCCESS! MySQL running (27377)
[root@xsbfwbtest mysql55]# ps -ef | grep mysql     #進程也出來了
root     27102     1  0 17:34 pts/2    00:00:00 /bin/sh /usr/local/mysql55/bin/mysqld_safe --datadir=/software/mysql --pid-file=/software/mysql/xsbfwbtest.pid
mysql    27377 27102  0 17:34 pts/2    00:00:00 /usr/local/mysql55/bin/mysqld --basedir=/usr/local/mysql55 --datadir=/software/mysql --plugin-dir=/usr/local/mysql55/lib/plugin --user=mysql --log-error=/software/mysql/xsbfwbtest.err --pid-file=/software/mysql/xsbfwbtest.pid --socket=/tmp/mysql.sock --port=3306
root     27445 14186  0 17:37 pts/2    00:00:00 grep --color=auto mysql
[root@xsbfwbtest mysql55]# service mysqld stop   #先停了,下面試下另一種方式啓動
Shutting down MySQL. SUCCESS!
[root@xsbfwbtest mysql55]# /usr/local/mysql55/bin/mysqld_safe --user=mysql --datadir=/software/mysql/ --basedir=/usr/local/mysql55/ &   #上面不設置成系統服務的話就直接用命令啓動
[1] 29239
[root@xsbfwbtest mysql55]# 190718 17:51:17 mysqld_safe Logging to '/software/mysql//xsbfwbtest.err'.
190718 17:51:17 mysqld_safe Starting mysqld daemon with databases from /software/mysql/

[root@xsbfwbtest mysql55]# /etc/init.d/mysqld status  #看下已經起來了
 SUCCESS! MySQL running (29525)
[root@xsbfwbtest mysql55]# netstat -tlnp | grep 3306   #監聽端口也出來咯
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      29525/mysqld
[root@xsbfwbtest mysql55]#

 下面玩下一下基本命令:

[root@xsbfwbtest mysql55]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.20-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;    #查看數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database sbwb;    #創建一個數據庫,名爲sbwb
Query OK, 1 row affected (0.01 sec)

mysql> use sbwb;  #進入數據庫
Database changed
mysql> show tables;   #查看該庫有幾張表
Empty set (0.00 sec)

mysql> create table a1 (id varchar(10),name varchar(10)); #建一張名爲a1的表,設字段和長度
Query OK, 0 rows affected (0.00 sec)

mysql> insert into a1 values ("1","test");  #向表插入數據
Query OK, 1 row affected (0.01 sec)

mysql> select * from a1;    #查看錶的內容
+------+------+
| id   | name |
+------+------+
| 1    | test |
+------+------+
1 row in set (0.00 sec)

mysql> select * from a1 where name='test';  #加個條件去查詢
+------+------+
| id   | name |
+------+------+
| 1    | test |
+------+------+
1 row in set (0.00 sec)

mysql> desc a1;  #查看a1表字段
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(10) | YES  |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table a1 modify column name varchar(20);  #修改表字段
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> desc a1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(10) | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> update a1 set name='debug' where id=1;   #更新表字段內容
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from a1;
+------+-------+
| id   | name  |
+------+-------+
| 1    | debug |
+------+-------+
1 row in set (0.00 sec)

mysql> delete from a1;   #清空表內容
Query OK, 1 row affected (0.01 sec)

mysql> select * from a1;  
Empty set (0.00 sec)

mysql> drop table a1;   #刪除表
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> drop database sbwb;  #刪除庫
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> show engines;   #查看存儲引擎
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
7 rows in set (0.00 sec)

mysql> show variables like '%storage_engine%';  #查看默認存儲引擎
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
| storage_engine         | InnoDB |
+------------------------+--------+
2 rows in set (0.00 sec)

mysql> show variables like '%char%';  #查看字符集
+--------------------------+------------------------------------+
| Variable_name            | Value                              |
+--------------------------+------------------------------------+
| character_set_client     | utf8                               |
| character_set_connection | utf8                               |
| character_set_database   | utf8                               |
| character_set_filesystem | binary                             |
| character_set_results    | utf8                               |
| character_set_server     | utf8                               |
| character_set_system     | utf8                               |
| character_sets_dir       | /usr/local/mysql55/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.00 sec)

mysql> alter table a1 engine=innodb;  #修改表a1存儲引擎,不過剛纔a1被我幹掉了,所以報錯
ERROR 1046 (3D000): No database selected
mysql> quit   #退出
Bye
[root@xsbfwbtest mysql55]#


下面修改密碼:

[root@xsbfwbtest mysql55]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.20-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> use mysql;   #用戶和密碼都放在mysql庫中的user表
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.01 sec)

mysql> update user set password = password('123456') where user='root';  #修改root密碼
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> flush privileges;   #刷新權限
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@xsbfwbtest mysql55]# mysql -u root -p  #使用root用戶
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.5.20-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

創建用戶以及授權

mysql> create database debug; 
Query OK, 1 row affected (0.01 sec)

mysql> grant all on debug.* to test@"%" identified by '123passwd';  #授權所有主機("%")通過test用戶和密碼('123passwd')訪問debug庫的所有權限(all)
Query OK, 0 rows affected (0.00 sec)

mysql> grant select,insert,update,delete on *.* to test@localhost identified by '123passwd';  #授權localhost主機通過test用戶和密碼('123passwd')訪問debug庫的查詢,插入,更新,刪除權限
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on debug.* to test@'192.168.0.2' identified by '123passwd';  #授權192.168.0.2主機通過test用戶和密碼('123passwd')訪問debug庫的所有權限(all)
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;   #別忘了要刷新權限纔會生效
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

[root@xsbfwbtest mysql55]# getenforce
Enforcing
[root@xsbfwbtest mysql55]# setenforce 0  #臨時關閉se
[root@xsbfwbtest mysql55]# getenforce
Permissive
[root@xsbfwbtest mysql55]# vim /etc/selinux/config   #配置開機關閉se,SELINUX=disabled
[root@xsbfwbtest mysql55]# systemctl stop firewalld   #關閉防火牆
[root@xsbfwbtest mysql55]# systemctl status firewalld  #查看防火牆狀態
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2019-07-18 19:27:14 CST; 1min 6s ago
     Docs: man:firewalld(1)
 Main PID: 850 (code=exited, status=0/SUCCESS)

Jul 17 19:06:33 xsbfwbtest systemd[1]: Starting firewalld - dynamic firewall daemon...
Jul 17 19:06:34 xsbfwbtest systemd[1]: Started firewalld - dynamic firewall daemon.
Jul 17 19:06:35 xsbfwbtest firewalld[850]: WARNING: ICMP type 'beyond-scope' is not supported by the kernel for ipv6.
Jul 17 19:06:35 xsbfwbtest firewalld[850]: WARNING: beyond-scope: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
Jul 17 19:06:35 xsbfwbtest firewalld[850]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
Jul 17 19:06:35 xsbfwbtest firewalld[850]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
Jul 17 19:06:35 xsbfwbtest firewalld[850]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
Jul 17 19:06:35 xsbfwbtest firewalld[850]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
Jul 18 19:27:13 xsbfwbtest systemd[1]: Stopping firewalld - dynamic firewall daemon...
Jul 18 19:27:14 xsbfwbtest systemd[1]: Stopped firewalld - dynamic firewall daemon.



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章