01、参考教程: CentOS下开启mysql远程连接,远程管理数据库, CentOS安装并设置MariaDB, MySQL 管理, centos7 安装Mariadb
02、安装MariaDB,默认依赖安装mariadb,一个是服务端、一个是客户端。
[root@epimetheus ~]# yum install mariadb-server -y
03、配置MariaDB
01)安装完成后首先要把MariaDB服务开启,并设置为开机启动
[root@epimetheus ~]# systemctl start mariadb # 开启服务
[root@epimetheus ~]# systemctl enable mariadb # 设置为开机自启动服务
02)首次安装需要进行数据库的配置
[root@epimetheus ~]# mysql_secure_installation
Enter current password for root (enter for none): # 输入数据库超级管理员root的密码(注意不是系统root的密码),第一次进入还没有设置密码则直接回车 Set root password? [Y/n] # 设置密码,y New password: # 新密码 Re-enter new password: # 再次输入密码 Remove anonymous users? [Y/n] # 移除匿名用户, y Disallow root login remotely? [Y/n] # 拒绝root远程登录,n,不管y/n,都会拒绝root远程登录 Remove test database and access to it? [Y/n] # 删除test数据库,y:删除。n:不删除,数据库中会有一个test数据库,一般不需要 Reload privilege tables now? [Y/n] # 重新加载权限表,y。或者重启服务也许
03)测试是否能够登录成功,出现 MariaDB [(none)]> 就表示已经能够正常登录使用MariaDB数据库了
[root@epimetheus ~]# mysql -u root -p
Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
04、远程链接mariadb数据库, 默认是拒绝root远程登录的。
1)系统防火墙
① 可以选择关闭防火墙
[root@epimetheus ~]# systemctl stop firewalld
② 在不关闭防火墙的情况下,允许某端口的外来链接。步骤如下,开启3306端口,重启防火墙
[root@epimetheus ~]# firewall-cmd --query-port=3306/tcp # 查看3306端口是否开启 no [root@epimetheus ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent # 开启3306端口 success [root@epimetheus ~]# firewall-cmd --reload # 重启防火墙 success [root@epimetheus ~]# firewall-cmd --query-port=3306/tcp # 查看3306端口是否开启 yes
③ 在阿里云CentOS中进行配置时,始终无法远程访问mariadb,最终发现阿里云控制台还有一层“防火墙”,即实例安全组。
2)在mysql中配置访问权限
① 先查看mysql数据库中的user表
[root@epimetheus ~]# mysql -u root -p # 先通过本地链接进入数据库 MariaDB [(none)]> use mysql; MariaDB [mysql]> select host, user from user; +-----------+------+ | host | user | +-----------+------+ | 127.0.0.1 | root | | ::1 | root | | epimetheus| root | +-----------+------+ rows in set (0.00 sec)
② 将与主机名相等的字段改为 "%" ,我的主机名为epimetheus,
MariaDB [mysql]> update user set host='%' where host='epimetheus'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [mysql]> select host, user from user; +-----------+------+ | host | user | +-----------+------+ | % | root | | 127.0.0.1 | root | | localhost | root | +-----------+------+ rows in set (0.00 sec)
③ 刷新权限表,或重启mariadb服务,一下二选一即可
MariaDB [mysql]> flush privileges; Query OK, 0 rows affected (0.00 sec) [root@epimetheus ~]# systemctl restart mariadb
注意:刷新权限表是在数据库中,重启服务是在外部命令行中
05、使用mysql时可能会使用到的命令
01) 在系统控制台中执行的命令
[root@epimetheus ~]# mysql -h 192.168.1.100 -P 3306 -u root -p #这个是远程登录的命令,使用账号和密码进入数据库 [root@epimetheus ~]# mysql -u root -p #这个是本机登录的命令,使用账号root和密码进入数据库
02) 在mysql命令行中, 可以输入并执行sql语句
--创建表 CREATE TABLE user_info (userId INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, userAge INTEGER); CREATE TABLE IF NOT EXISTS user_info (userId INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, userAge INTEGER); CREATE TABLE user_info (userId INTEGER, userName TEXT, userAge INTEGER, PRIMARY KEY(userId, userName)); --销毁表 DROP TABLE user_info; --修改表, 增加一列 ALTER TABLE tab_name ADD COLUMN col_name TEXT; --修改表, 修改表名, 注意:新的表名会被带上双引号 ALTER TABLE tab_name RENAME TO new_tab_name; --删除项 DELETE FROM user_info WHERE userName='saber'; --添加项 INSERT INTO user_info(userName, userAge) VALUES('saber', 18); INSERT INTO user_info(userName, userAge) VALUES('saber', 18), ('baserker', 22); INSERT INTO mo_personmap (personid, belongid) SELECT b.id, b.departid FROM mo_person b; --修改项 UPDATE user_info SET userAge=16 WHERE userName='saber'; --查询项 SELECT *FROM user_info;
03) 还可以执行mysql程序自己的命令, 例如:
mysql> grant all privileges on *.* to 'root'@'%' identified by 'mypassword' with grant option; #设置账号root可以被任何ip行使所有权限 mysql> flush privileges; #使权限设置生效 mysql> show databases; #显示所有存在的数据库 mysql> use mydatabase; #选择指定的数据库 mysql> show tables; #显示所有的表 mysql> show columns from mytable; #显示表结构设计 mysql> exit; #退出mysql