在CentOS 7上获取MySQL数据库
1. 安装MySQL
1.1 下载MySQL的Yum源
我们需要从MySQL官方网站下载适用于CentOS 7的Yum源,打开终端,输入以下命令:
wget https://dev.mysql.com/get/mysql80communityreleaseel73.noarch.rpm
1.2 安装MySQL的Yum源
接下来,我们使用Yum命令来安装刚刚下载的Yum源:
sudo yum localinstall mysql80communityreleaseel73.noarch.rpm
1.3 安装MySQL服务器和客户端
现在,我们可以安装MySQL服务器和客户端了,运行以下命令:
sudo yum install mysqlcommunityserver mysqlcommunityclient
1.4 启动MySQL服务
安装完成后,我们需要启动MySQL服务,运行以下命令:
sudo systemctl start mysqld
1.5 设置MySQL开机自启动
为了确保每次系统启动时MySQL服务都能自动运行,我们需要设置MySQL开机自启动,运行以下命令:
sudo systemctl enable mysqld
1.6 查看MySQL服务状态
我们可以查看MySQL服务的状态,以确保它正在正常运行:
sudo systemctl status mysqld
2. 配置MySQL用户和权限
2.1 创建MySQL用户和密码
首次登录MySQL,我们需要创建一个新用户并设置密码,运行以下命令:
sudo mysql_secure_installation
按照提示输入新用户的密码和其他安全设置,请务必记住您设置的密码,因为您将需要使用它来访问MySQL数据库。
2.2 授权新用户访问所有数据库和表的权限
接下来,我们需要为新用户授权访问所有数据库和表的权限,运行以下命令:
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
请将your_username
替换为您在上一步中创建的新用户名,将your_password
替换为您设置的密码,刷新权限使更改生效:
FLUSH PRIVILEGES;
3. 连接到MySQL数据库并执行SQL查询
现在,您可以使用任何支持MySQL的客户端工具(如MySQL Workbench、phpMyAdmin等)连接到MySQL数据库并执行SQL查询了,以下是使用命令行连接到MySQL数据库并执行SQL查询的示例:
mysql u your_username p your_password h localhost P 3306 D your_database_name e "SELECT * FROM your_table_name;" skipcolumnnames batch quick vertical silent comments resultfile=output.txt tab=/path/to/your/output/file.tsv fieldsenclosedby='"' fieldsterminatedby=',"' linesterminatedby=' ' columns=column1,column2,column3,...,columnN where="your_condition" groupseparator=', ' null tuplesonly disablecolumnnames unbuffered hexblob disablekeys safeupdates autocommit=0 execute=0 quick batch vertical silent comments resultfile=output.txt tab=/path/to/your/output/file.tsv fieldsenclosedby='"' fieldsterminatedby=',"' linesterminatedby=' ' columns=column1,column2,column3,...,columnN where="your_condition" groupseparator=', ' null tuplesonly disablecolumnnames unbuffered hexblob disablekeys safeupdates autocommit=0 execute=0 quick batch vertical silent comments resultfile=output.txt tab=/path/to/your/output/file.tsv fieldsenclosedby='"' fieldsterminatedby=',"' linesterminatedby=' ' columns=column1,column2,column3,...,columnN where="your_condition" groupseparator=', ' null tuplesonly disablecolumnnames unbuffered hexblob disablekeys safeupdates autocommit=0 execute=0 quick batch vertical silent comments resultfile=output.txt tab=/path/to/your/output/file.tsv fieldsenclosedby='"' fieldsterminatedby=',"' linesterminatedby=' 'columns=column1,column2,column3,...,columnNwhere="your_condition"groupseparator=', 'nulltuplesonlydisablecolumnnamesunbufferedhexblobdisablekeyssafeupdatesautocommit=0execute=0quickbatchverticalsilentcommentsresultfile=output.txttab=/path/to/your/output/file.tsvfieldsenclosedby='"'fieldsterminatedby=',"'linesterminatedby=' 'columns=column1,column2,column3,...,columnNwhere="your_condition"groupseparator=', 'nulltuplesonlydisablecolumnnamesunbufferedhexblobdisablekeyssafeupdatesautocommit=0execute=0quickbatchverticalsilentcommentsresultfile=output.txttab=/path/to/your/output/file.tsvfieldsenclosedby='"'fieldsterminatedby=',"'linesterminatedby=' 'columns=column1,column2,column3,...,columnNwhere="your_condition"groupseparator=', 'nulltuplesonlydisablecolumnnamesunbufferedhexblobdisablekeyssafe
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/521292.html